class SuperClass
{
public SuperClass()
{
}
public SuperClass(String s)
{
s=" ";
}
public void doSomething()
{
System.out.println("这是SuperClass输出的!");
}
}
class SubClass1 extends SuperClass
{
public void doSomething()
{
System.out.println("这是SubClass1输出的!");
}
public void doSth1()
{
System.out.println("这是doSth1输出的!");
}
}
class SubClass2 extends SuperClass
{
public void doSomething()
{
System.out.println("这是SubClass2输出的!");
}
public void doSth1()
{
System.out.println("这是doSth2输出的!");
}
}
public class testLayer
{
public static void main(String arge[])
{
SuperClass x = new SuperClass();
SuperClass y = new SubClass1();
SuperClass z = new SubClass2();
x.doSomething();
y.doSth1();
z.doSth2();
}
}
请高手改正!!谢谢!
------解决方案--------------------
- Java code
class SuperClass { public SuperClass() { } public SuperClass(String s) { s = " "; } public void doSomething() { System.out.println("这是SuperClass输出的!"); } public void doSth1() { // TODO Auto-generated method stub }}class SubClass1 extends SuperClass { public void doSomething() { System.out.println("这是SubClass1输出的!"); } public void doSth1() { System.out.println("这是doSth1输出的!"); }}class SubClass2 extends SuperClass { public void doSomething() { System.out.println("这是SubClass2输出的!"); } public void doSth1() { System.out.println("这是doSth2输出的!"); }}public class testLayer { public static void main(String arge[]) { SuperClass x = new SuperClass(); SuperClass y = new SubClass1(); SuperClass z = new SubClass2(); x.doSomething(); y.doSomething(); z.doSomething(); y.doSth1(); z.doSth1(); }}
------解决方案--------------------
- Java code
public static void main(String arge[]) { SuperClass x = new SuperClass(); SuperClass y = new SubClass1()f");try{fff b=new fff();b.method();}catch(NoSuchMethodException e){e.printStackTrace();}}}class fff{}
------解决方案--------------------
- Java code
SuperClass z = new SubClass2();
------解决方案--------------------
你重写父类的方法,用父类的引用是可以调用的.而且调用的是子类里的方法,父类的方法被屏蔽了.但是父类看不到子类里的方法,所以父类的引用指向子类的对象,也调用不到子类里的方法的.只能子类自己的对象调用自己的方法.
- Java code
class SuperClass { public SuperClass() { } public SuperClass(String s) { s = " "; } public void doSomething() { System.out.println("这是SuperClass输出的!"); }}class SubClass1 extends SuperClass { public void doSomething() { System.out.println("这是SubClass1输出的!"); } public void doSth1() { System.out.println("这是doSth1输出的!"); }}class SubClass2 extends SuperClass { public void doSomething() { System.out.println("这是SubClass2输出的!"); } public void doSth2() { System.out.println("这是doSth2输出的!"); }}public class testLayer { public static void main(String arge[]) { SuperClass x = new SuperClass(); SuperClass y = new SubClass1(); SuperClass z = new SubClass2(); SubClass1 a = new SubClass1(); SubClass2 b = new SubClass2(); x.doSomething(); y.doSomething();//父类的方法被屏蔽了,调用的是子类里的方法 z.doSomething();//父类的方法被屏蔽了,调用的是子类里的方法 //y.doSth1();这里是不可见的,所以不能调用 //z.doSth2();这里是不可见的,所以不能调用 a.doSth1(); b.doSth2(); }}