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();
}
}
![运行结果](https://img-blog.csdnimg.cn/20181222144322932.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDE0NDAwMw==,size_16,color_FFFFFF,t_70)
一、重写
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();
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20181222175131180.png)