A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© pengyu 中级黑马   /  2014-6-4 16:21  /  824 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class B {
private int radius = 10;
public void draw() {
System.out.println(“B.draw(), radius = ” + radius);
}
public B() {
System.out.println(“B constructor”);
draw();
}
}
public class A extends B {
private int radius = 1;
public void draw() {
System.out.println(“A.draw(), radius = ” + radius);
}
public A(int radius) {
this.radius = radius;
System.out.println(“A constructor”);
}
public static void main(String[] args) {
A a = new A(5);
}
}

2 个回复

倒序浏览
你觉得运行结果什么地方不能理解?
回复 使用道具 举报 1 0
  1. public class Test9 {
  2.         public static void main(String[] args){
  3.                 new GoodStudent();//创建GoodStudent对象  执行了person的构造方法  
  4.         }
  5. }

  6. class Person{
  7.         public Person(){
  8.                 System.out.println("调用了Person的无参构造方法");
  9.         }
  10.        
  11. }
  12. class Student extends Person{
  13.         public Student(){
  14.                 System.out.println("调用了student的无参构造");
  15.         }
  16.         public Student(String str){
  17.                 System.out.println("调用了Student的有参构造方法-参数为:"+str);
  18.         }
  19.        
  20. }
  21. class GoodStudent extends Student{
  22.         public GoodStudent(){
  23.                
  24.                 super("nihao");//可以指定调用哪个Student的构造方法,不能指定person的构造方法
  25.                 System.out.println("创建了GoodStudent,使用了GoodStudent的无参构造方法");
  26.         }
  27.        
  28. }
复制代码

这是我写的代码 你看看 我不知道你到底是想问什么   我这个通过super指定调用父类的哪一个构造方法  
关于你的那个代码  如果子类没有覆盖父类的方法(你的代码中 父类的方法就是draw) 当调用方法时  就是调用父类的方法 如果在把父类的方法覆盖了 就会调用自己的方法 不会调用父类的方法 如果想要调用父类的方法 需要去创建父类对象来实现

评分

参与人数 1技术分 +1 收起 理由
SyouRai_Tsk + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马