日期:2014-05-20  浏览次数:20678 次

求java的实现继承的一个列子?
A   输出1
B 输出2
要能用的

------解决方案--------------------
class Parent{
public Parent(){
System.out.println( "1 ");
}
}
public class Child extends Parent{
public Child(){
System.out.println( "2 ");
}
public static void main(String[] args) {
new Child();
}
}

------解决方案--------------------
class Parent{
public Parent(){
public void test(){
System.out.println( "1 ");
}
}
}

public class Child extends Parent{
public Child(){
//覆盖上层方法
public void test(){
System.out.println( "2 ");
}
}
}


test.java:
public static void main(String[] args) {
Parent a = new Parent();
Child b = new Child();

a.test();
b.test();

}
------解决方案--------------------
//父类
class Parent{
public Parent(){
private void test(){
System.out.println( "1 ");
}
}
}
//子类继承父类
class Child extends Parent{
public Child(){
//覆盖上层方法
private void test(){
System.out.println( "2 ");
}
}
}


public class test{
public static void main(String[] args) {
Parent a = new Parent();
Child b = new Child();

a.test();
b.test();

}
}

------解决方案--------------------
public class Test {
  public static void main(String[] args) {
    Parent p1 = new Parent();
    Parent p2 = new Child();
    p1.test();
    p2.test();
  }
}

class Parent {
  public void test() {
    System.out.println( "My name is Parent. ");
  }
}

class Child extends Parent {
  public void test() {
    System.out.println( "My name is Child. ");
  }
}
------解决方案--------------------
你照着”蟑螂“的代码自己重新打一遍,不要ctrl+c,他上面的代码带空格~
------解决方案--------------------
学习
------解决方案--------------------
class Parent
{
Parent(){}
void show(){
System.out.println( "1 ");
}
}
class Child extends Parent
{Child(){}
void disp(){
System.out.println( "2 ");}
}
class ParentTest{
public static void main(String[] args)
{
Parent a = new Parent();
Child b = new Child();

a.show();
b.disp();

}
}

------解决方案--------------------
还是用IDE方便~
------解决方案--------------------
用eclipse,再把他们的代码敲进去,复制总是会出现一些意外的!