如果A继承B,B继承C,那么怎样才能在A中实现调用C的构造方法。

解决方案 »

  1.   

    supper.方法名。   构造方法就免了‘.方法名’
      

  2.   

    不知道你是不是这个意思。public class A extends B {
    A() {
    System.out.println("A");
    }
    public static void main(String[] args) {
    new A();
    }
    }
    class B extends C {
    B() {
    System.out.println("B");
    }
    }class C {
    C() {
    System.out.println("C");
    }
    }
    ================》》
    C
    B
    A
      

  3.   

    我的意思是在B中显示的调用C的无参数的构造方法,具体代码是:super();
    同样的道理,我想要的是A中也显式的调用C的这个无参数的构造方法。我知道如果在B中显式的用了super()方法,在C的构造方法中也用到了super(),单继承嘛,C其实在实例的时候也会执行A中的对应的构造函数中的内容。这都不是问题的关键,关键是我想知道,能不能在A中也显式地调用C的构造方法,如果能用这样的方式。
      

  4.   

    我的意思是在B中显示的调用C的无参数的构造方法,具体代码是:super();
    同样的道理,我想要的是A中也显式的调用C的这个无参数的构造方法。我知道如果在B中显式的用了super()方法,在C的构造方法中也用到了super(),单继承嘛,C其实在实例的时候也会执行A中的对应的构造函数中的内容。这都不是问题的关键,关键是我想知道,能不能在A中也显式地调用C的构造方法,如果能用这样的方式。
      

  5.   

    public class HelloWorld {
    public static void main(String[] args) {
    new A().display();
    }
    }class C {
    int x; public C() {
    x = 1;
    }}class B extends C {
    int y; public B() {
    super();
    y = 2;
    }}class A extends B {
    int z; public A() {
    super();
    z = 3;
    } public void display() {
    System.out.println(x);
    System.out.println(y);
    System.out.println(z); }}
    逐层调用父类的构造器.
    结果为:1 2 3
      

  6.   

     可以的,但是调用无参构造,一般打印输出没有明显的结果,如果有参比较明显:
     
     
      class Person{    //Person类
    private String name;
    private int age;
    public Person(String name,int age){ //Person的构造方法
      this.setName(name);
      this.setAge(age);
    }
    public void setName(String name){
          this.name=name;
    }
    public void setAge(int age){
      this.age=age;
    }
    public String getName( ){
      return this.name;
    }
    public int getAge(){
      return this.age;
    }
    }
    class Student extends Person{   //Student类,继承Person类
    private float score;        //如果调用Person类的构造方法加上 private float score;
    public Student(String name,int age,float score){   //Student类构造方法
      super(name,age);   //调用Person类的构造方法
      this.setScore(score);
    }
    public void setScore(float score){
          this.score=score;
    }
    public float getScore( ){
      return this.score;
    }
    }
    class Pupil extends Student{   //Pupil类,继承Student类
    private float height;
    public Pupil(String name,int age,float score,float height){   //Pupil类构造方法
      /*调用Student类的构造方法,也可以调用Person类的代码如下:
        Super(name,age);*/   super(name,age,score);   
          this.setHeight(height);
    }
    /*如果调用Person类的构造方法,则加入score的setter与getter方法:
      public void setScore(float score){
          this.score=score;
    }
    public float getScore( ){
      return this.score;
    }但明显代码重复,不建议使用
    */
    public void setHeight(float height){
      this.height=height;
    }
    public float getHeight(){
      return this.height;
    }
    }
    public class Demo{
    public static void main(String args[]){
      Pupil p=new Pupil("嘻嘻",10,100.0f,128.0f);   //实例化Pupil对象
      System.out.println("姓名:"+p.getName()+"\t年龄:"+p.getAge()+       
                     "\t成绩:"+p.getScore()+"\t身高:"+p.getHeight());  //打印输出
    }
    }
      

  7.   

    class Person{    //Person类
    private String name;
    private int age;
    public Person(String name,int age){ //Person的构造方法
      this.setName(name);
      this.setAge(age);
    }
    public void setName(String name){
          this.name=name;
    }
    public void setAge(int age){
      this.age=age;
    }
    public String getName( ){
      return this.name;
    }
    public int getAge(){
      return this.age;
    }
    }
    class Student extends Person{   //Student类,继承Person类
    private float score;        
    public Student(String name,int age,float score){   //Student类构造方法
      super(name,age);   //调用Person类的构造方法
      this.setScore(score);
    }
    public void setScore(float score){
          this.score=score;
    }
    public float getScore( ){
      return this.score;
    }
    }
    class Pupil extends Student{   //Pupil类,继承Student类
    private float height;     //如果调用Person类的构造方法加上 private float score;
    public Pupil(String name,int age,float score,float height){   //Pupil类构造方法
      /*调用Student类的构造方法,也可以调用Person类的代码如下:
        Super(name,age);*/   super(name,age,score);   
          this.setHeight(height);
    }
    /*如果调用Person类的构造方法,则加入score的setter与getter方法:
      public void setScore(float score){
          this.score=score;
    }
    public float getScore( ){
      return this.score;
    }但明显代码重复,不建议使用
    */
    public void setHeight(float height){
      this.height=height;
    }
    public float getHeight(){
      return this.height;
    }
    }
    public class Demo{
    public static void main(String args[]){
      Pupil p=new Pupil("嘻嘻",10,100.0f,128.0f);   //实例化Pupil对象
      System.out.println("姓名:"+p.getName()+"\t年龄:"+p.getAge()+       
                     "\t成绩:"+p.getScore()+"\t身高:"+p.getHeight());  //打印输出
    }
    }
     嵌入Java代码看起来好些,改了一个小错误://如果调用Person类的构造方法加上 private float score; 加到private float height; 这一行。  希望对楼主有用。
      

  8.   

    class Person{    //Person类 
    private String name; 
    private int age; 
    public Person(String name,int age){ //Person的构造方法 
      this.setName(name); 
      this.setAge(age); 

    public void setName(String name){ 
          this.name=name; 

    public void setAge(int age){ 
      this.age=age; 

    public String getName( ){ 
      return this.name; 

    public int getAge(){ 
      return this.age; 


    class Student extends Person{  //Student类,继承Person类 
    private float score;        
    public Student(String name,int age,float score){  //Student类构造方法 
      super(name,age);  //调用Person类的构造方法 
      this.setScore(score); 

    public void setScore(float score){ 
          this.score=score; 

    public float getScore( ){ 
      return this.score; 


    class Pupil extends Student{  //Pupil类,继承Student类 
    private float height;    //如果调用Person类的构造方法加上 private float score; 
    public Pupil(String name,int age,float score,float height){  //Pupil类构造方法 
      /*调用Student类的构造方法,也可以调用Person类的代码如下: 
        Super(name,age);*/   super(name,age,score);  
          this.setHeight(height); 

    /*如果调用Person类的构造方法,则加入score的setter与getter方法: 
      public void setScore(float score){ 
          this.score=score; 

    public float getScore( ){ 
      return this.score; 
    }但明显代码重复,不建议使用 
    */ 
    public void setHeight(float height){ 
      this.height=height; 

    public float getHeight(){ 
      return this.height; 


    public class Demo{ 
    public static void main(String args[]){ 
      Pupil p=new Pupil("嘻嘻",10,100.0f,128.0f);  //实例化Pupil对象 
      System.out.println("姓名:"+p.getName()+"\t年龄:"+p.getAge()+      
                    "\t成绩:"+p.getScore()+"\t身高:"+p.getHeight());  //打印输出 


      

  9.   

    貌似java不支持多继承,你可以把A定义接口,然后再实现继承
      

  10.   

    在Java中要实例化子类对象,会自动调用父类的构造方法。
      

  11.   

    class C
    {
    public C()
    {
    System.out.println("C中构造方法!");
    }
    };
    class B extends C
    {
    public B()
    {
    System.out.println("B中构造方法!");
    }
    };
    public class A extends B
    {
    public static void main(String[] args)
    {
    new A();
    }
    };
    结果是:
    C中构造方法!
    B中构造方法!