class Animal2
{
public String getDesc()
{
return "Animal";
}
public String toString()
{
return getDesc();
}
}
public class Wolf2 extends Animal2
{
//定义name、weight两个实例变量
private String name;
private double weight;
public Wolf2(String name , double weight)
{
//为name、weight两个实例变量赋值
this.name = name;
this.weight = weight;
}
//重写父类的getDesc()方法
@Override
public String getDesc()
{
return "Wolf[name=" + name + " , weight="
+ weight + "]";
}
public static void main(String[] args)
{
System.out.println(new Wolf2("灰太郎" , 32.3));
}
} 这样子将会使用子类自己的方法
{
public String getDesc()
{
return "Animal";
}
public String toString()
{
return getDesc();
}
}
public class Wolf2 extends Animal2
{
//定义name、weight两个实例变量
private String name;
private double weight;
public Wolf2(String name , double weight)
{
//为name、weight两个实例变量赋值
this.name = name;
this.weight = weight;
}
//重写父类的getDesc()方法
@Override
public String getDesc()
{
return "Wolf[name=" + name + " , weight="
+ weight + "]";
}
public static void main(String[] args)
{
System.out.println(new Wolf2("灰太郎" , 32.3));
}
} 这样子将会使用子类自己的方法