1. 程式人生 > >Java基礎案例002

Java基礎案例002

Java基礎案例002

一、構造方法

public class Text {
 public String name;
 public int a;
 public int b;
 public int c;
 public void print() {
  System.out.println(name+"的攻擊力為:"+a);
  System.out.println(name+"的防禦力為:"+b);
  System.out.println(name+"的生命值為:"+c);
 }
}
class luna{
 public static void main(String[
] args) { Text aa=new Text(); aa.a=50; aa.b=70; aa.c=200; aa.name="露娜"; aa.print(); System.out.println("-------------------"); Text bb=new Text(); bb.a=40; bb.b=30; bb.c=500; bb.name="亞瑟"; bb.print(); } }

執行結果

一、重寫

class Text1 {
 String sname;
 int level;
 int snumber;
 void print
() { System.out.println("英雄名:"+sname+";等級:"+level+";戰鬥力:"+snumber); } Text1(String name,int lev,int number){ sname=name; level=lev; snumber=number; } } class Cxietwo extends Text1{ String sadder; Cxietwo(String x,int y,int z,String aa){ super(x,y,z); sadder=aa; } void print() { System.
out.println("英雄名:"+sname+";等級:"+level+";戰鬥力:"+snumber+";稱號:"+sadder); } } class Text{ public static void main(String args[]){ Cxietwo a1=new Cxietwo("風魔",50,580500,"四方殺神"); a1.print(); } }

在這裡插入圖片描述