子类不继承父类的构造方法,但父类的构造方法对子类构造方法的创建有影响。具体来说就是:①.当父类没有无参构造方法时,子类也不能有无参构造方法;且必须在子类构造方法中显式以super(参数)的形式调用父类构造方法。否则会出现如下的错误:Implicit super constructor Person() is undefined for default constructor. Must define an explicit constructor子类在有参构造方法中显式调用super(参数)后,如果再写一个无参构造方法...
1java中:2import java.util.Scanner;3publicclass ClassTest{4publicstaticvoid main(String args[]){5 child ch=new child(2);6 parent p=ch;7 p.print();8//p.print2();//调用错误,父类中没有改成员方法,该方法只属于子类! 9 }
10}
1112class parent{
13int xx;
14 parent(int x){
15 xx=x;
16 }
17void print(){
18 System.out.println("this is parent!");
19 }
20int f(){
21int ...
成员变量的隐藏和方法的重写Goods.javapublicclass Goods {publicdouble weight;publicvoid oldSetWeight(double w) {weight=w;System.out.println("double型的weight="+weight);}publicdouble oldGetPrice() {double price = weight*10;return price;}
}CheapGoods.javapublicclass CheapGoods extends Goods {publicint weight;publicvoid newSetWeight(int w) {weight=w;System.out.println("int型的weight="+weight);}publicdo...
类索引(this_class)和父类索引(super_class)都是u2类型的数据,而接口索引(interfaces)是一组u2类型的数据集合,class文件中由这三项数据来确定这个类的继承关系。类索引用于确定这个类的全限定名,父类索引用于确定这个类的父类的全限定名。由于Java语言不允许多继承,所以父类索引只有一个,除了java.lang.Object之外,所有的Java类都有父类,因了除了java.lang.Object之外,所有Java类的父类索引都不为0。接口索引集合用来描述这...
一、父类引用指向子类对象时 1、若子类覆盖了某方法,则父类引用调用子类重新定义的新方法2、若子类未覆盖某方法,则父类引用调用父类本身的旧方法3、若子类覆盖了某属性,但父类引用仍调用父类本身的旧属性4、若子类未覆盖某属性,则父类引用调用父类本身的旧属性5、父类引用不能访问子类新定义的方法 二、子类引用指向自身对象时 1、若子类覆盖了某方法,则子类引用调用子类重新定义的新方法2、若子类未覆盖某方法,则子类引用调...
class Fu{ int a=2; Fu() { System.out.println("fu run"); }}class Zi extends Fu{ Zi() { System.out.println("zi run"); }}public class Copy { public static void main(String[] args) { Zi zi = new Zi(); System.out.println(zi.a); }}运行结果为 fu run ,zi run ,0 说明子类在实例化的时候,调取了父类的构造函数中的方法,但并没有调用成员变量。,====== 子类 实例化的时候 默认调用父类为空的构造方法 即super...
Java学习1-子类实例化时调用父类构造函数时this指向
Java久疏战阵,重新拾起碰到了不少问题,以前的基础知识忘得差不多了,趁着不能出门多学习下。
子类通过new的形式实例化时,会调用父类的构造,那么此时在父类中的this关键字指向的是谁呢?通过实际代码来看看。
父类Animalpublic class Animal {private String name;private Integer age;public void print(){System.out.println("super class");}public Animal(String name,...
例如,我有A类,A有子类B和C,我想随机创建A,B或C,我可以使用这样的条件语句:A a;
switch(new java.util.Random(3).nextInt()){case 0:a=new A();break;case 1:a=new B();break;case 2:a=new C();break;
}
a.doSomething();但我希望有一个更好的维护版本,很容易添加新的子类,然后我尝试使用数组来保存每个类的.class对象,但由于“不兼容的类型”,它无法编译:Class[] array={A.class,B.class,C.class};
A a;
try{a=array[new java.ut...
1. parent父类与child子类public class parent {public void add(){System.out.println("A add...");}public void addAll(){System.out.println("A add ALL...");this.add();-----------------------------------------这里调用的是子类this.hello();---------------------------------------这里调用的是父类System.out.println(this.getClass());}public void hello(){System.out.println("hello");}}public class child extends...
实例来源:《Java编程思想》,博客链接:Java编程思想1-15章笔记案例代码:
class A{public A(){System.out.println("1.父类A的构造方法");}{System.out.println("2.父类A的构造代码块");}static{System.out.println("3.父类A的静态代码块");}
}
public class B extends A{public B(){System.out.println("4.子类B的构造方法");}{System.out.println("5.子类B的构造代码块");}static{System.out.println("6.子类B的静态代码块");}/...
public class Parent {static {System.out.println("parent static");}{System.out.println("parent code block");}public Parent(String name){System.out.println("parent init");}
}public class Child extends Parent {static {System.out.println("child static");}{System.out.println("child code block");}public Child(){System.out.println("child init");}public void echo(){System.out.println("this is child");}pub...
1、子类重写的方法的方法名和形参列表必须与父类被重写的方法相同。 2、子类重写的方法的权限修饰符不能小于父类被重写的方法,且子类不能对父类的private方法进行重写。 3、对于返回值类型,若父类被重写的方法是void,则子类重写的方法只能是void;若父类被重写的方法是其他类型,则子类重写的方法只能是该类型或该类型的子类;若父类被重写的方法是基本数据类型,则子类重写的方法只能与父类相同。 4、子类重写的方法抛出的异常...
instanceof 判断两者是否有继承关系
调用类
package Demo04;?public class Application { public static void main(String[] args) { //instanceof 一条线的线性指向关系,在这条线的为true //主要与左边的类型有关,右边为指向关系 //Object>Person>Student || Teacher Student student = new Student(); System.out.println(student instanceof Student);//true System.out.printl...
继承(子类可以使用父类的所有资源,也就是继承了财产,JAVA单继承只有一个父亲,但是可以子子孙孙无穷尽也)
第一步:子类 extends 父类继承财产,查看结构ctrl+h(extends,子类是父类的拓展类,是is关系)第二步:使用财产第三步:所有类的祖先Object,所有类都继承Objec类不显示extends默认继承,都由Object的七个方法IDEA:查看Object类:crtl+确认JDK文档:查看Object类
注:通俗点说,就是一个父亲可以有多个不同性格,不同能力的儿子,这些儿子不仅可以做父亲能做的事情,而且也可以做
父亲不能做的事情。即是扩展和延伸!