Constructors (§8.8) and type variables (§4.4) are not members.所以不能被继承 构造方法本身是static final的方法???? Constructors是可以被重载的,怎么可能是final!!! Constructors在其中是可以访问本类的非静态属性,能使用this,所以他也不是static 方法不能被继承???????? # Members inherited from its direct superclass (§8.1.4), except in class Object, which has no direct superclass # Members inherited from any direct superinterfaces (§8.1.5) # Members declared in the body of the class (§8.1.6) The members of a class type (§8.2) are classes (§8.5, §9.5), interfaces (§8.5, §9.5), fields (§8.3, §9.3, §10.7), and methods (§8.4, §9.4). Members are either declared in the type, or inherited because they are accessible members of a superclass or superinterface which are neither private nor hidden nor overridden (§8.4.8).
构造函数不能继承 ??? 谁说的??? 如果子类的构造方法中没有显示地调用父类的构造方法,系统默认调用父类无参的构造方法 如果子类的构造方法中没有显示地调用父类的构造方法,而父类又没有无参的构造方法,则编译出错! class Person { private int age; public Person(int age) { this.age = age; } }
class Student extends Person{ private String name; public Student(String name,int age) { super(age); //如果没有这句话,编译会出错! 因为在父类中定义了一个构造方法 this.name = name; } } public class TestInherit { public static void main(String[] args) { Person p1 = new Person(12); Student stu = new Student("tom",14); } }
重载跟重写都没分清呢.."Constructors是可以被重载的,怎么可能是final!!! "
把extends理解成扩展而不是继承的意思 就好理解多了
这题要是猛一问,还真有点晕:我觉得,LZ关于“构造器继承”的意思可以用“方法继承”的意思来理解:LZ的意思是:如果class A extends B, 子类A中没有B的构造器public B(){},但由于父类B具有。所以A继承了B的这个构造器,我们就可以这样写了 A a=new B(); 当然,谁都知道这是不可以的。构造器并不是方法。至于为什么? (1) 通俗的解释 可以参照楼11 (2) 深奥点的解释 可以参照楼15总之,不要把构造器看成是类的成员方法,这绝对是两码事。
这样理解更好 class Person{
Person() { System.out.println("父类构造函数"); } Person(int a) { System.out.println("带参数的父类构造函数"); }
} public class Test extends Person {
public Test() {
System.out.println("子类构造函数"); }
public Test(int a) { Person(12);//错误吧 只能用super(12)吧,如果能继承的话就应该可以写成Person(12); System.out.println("带参数的子类构造函数"); } public static void main(String[] args) { Test a=new Test(12);
但是类就不同了,动物这个类可以被狗这个类继承.你这样理解就好了,构造函数是针对对象的(就是我举例那个动物实体一样),除了构造函数其他都是针对抽象的,也就是类(动物没实体化之前就是一种抽象,)就这么简单
构造方法本身是static final的方法????
Constructors是可以被重载的,怎么可能是final!!!
Constructors在其中是可以访问本类的非静态属性,能使用this,所以他也不是static
方法不能被继承????????
# Members inherited from its direct superclass (§8.1.4), except in class Object, which has no direct superclass
# Members inherited from any direct superinterfaces (§8.1.5)
# Members declared in the body of the class (§8.1.6)
The members of a class type (§8.2) are classes (§8.5, §9.5), interfaces (§8.5, §9.5), fields (§8.3, §9.3, §10.7), and methods (§8.4, §9.4). Members are either declared in the type, or inherited because they are accessible members of a superclass or superinterface which are neither private nor hidden nor overridden (§8.4.8).
如果子类的构造方法中没有显示地调用父类的构造方法,系统默认调用父类无参的构造方法
如果子类的构造方法中没有显示地调用父类的构造方法,而父类又没有无参的构造方法,则编译出错! class Person {
private int age;
public Person(int age) {
this.age = age;
}
}
class Student extends Person{
private String name;
public Student(String name,int age) {
super(age); //如果没有这句话,编译会出错! 因为在父类中定义了一个构造方法
this.name = name;
}
} public class TestInherit {
public static void main(String[] args) {
Person p1 = new Person(12);
Student stu = new Student("tom",14);
}
}
(1) 通俗的解释 可以参照楼11
(2) 深奥点的解释 可以参照楼15总之,不要把构造器看成是类的成员方法,这绝对是两码事。
class Person{
Person() {
System.out.println("父类构造函数");
}
Person(int a) {
System.out.println("带参数的父类构造函数");
}
}
public class Test extends Person {
public Test() {
System.out.println("子类构造函数");
}
public Test(int a) {
Person(12);//错误吧 只能用super(12)吧,如果能继承的话就应该可以写成Person(12); System.out.println("带参数的子类构造函数");
} public static void main(String[] args) {
Test a=new Test(12);
}}
构造函数既不是static final的,因为static修饰的会在类加载的时候,就加载static修饰的静态块和其他静态成员,做个测试看就知道,在加载类的时候(即是用new创建对象前)构造函数里没有反应,前提是你在构造函数里别定义static的变量,不然他也会被加载的,这就排除了是static的可能。final的嘛,用在方法的前面标示该方法不能被重写,却没有说是不能被继承吧;但是构造函数前面的修饰符一般就只有哪几种,如果是public和procted,和无修饰符时,说明子类能够继承并能使用,但是当你在用方法的时候却没能发现父类这样的构造函数出现,也排除了构造函数能被继承的说法;你还会说pirvate修饰的时候,也不能发现从子类继承的方法吧,若果是这样,当你不是private的时候继承父类不会出错,然是private的时候会报错,两者一比较不就知道也不可能是private了嘛。