class SuperClass
{
String a="hello";
void test()
{
System.out.println(a);
}
}
public class SubClass extends SuperClass
{
String a="aaa";
public static void main (String args[])
{
SubClass bar=new SubClass();
bar.test();
System.out.println(bar.a);
}
}
为什么 //output: hello
// aaa
下面的程序能输出派生类的Square,为什么上面的不能输出派生类的aaa
public class Rectangle
{
String name;
double length;
double width;
public Rectangle(double _length,double _width)
{
name="Rectangle";
length=_length;
width=_width;
}
public void display()
{
System.out.println( name);
System.out.println( "length:"+length);
System.out.println( "width:"+width);
}
}
public class Square extends Rectangle
{
public Square(double _side)
{
super(_side,_side);
name="Square";
}
}
public class Test
{
public static void main(String args[])
{
Square square=new Square(4);
square.display();
}
}