- class Fu
- {
- private String name;
- private int age;
- private String address;
- Fu(String name,int age) //父类中没有空的构造函数。这时就需要根据子类中的super()传递的值来选择父类的构造函数
- {
- this.name = name;
- this.age = age;
- }
- Fu(String address)
- {
- this.address = address;
- }
- public void print()
- {
- System.out.println("name : "+name+" age : "+age);
- }
- public void printAddress()
- {
- System.out.println("address : "+address);
- }
- }
- class Zi extends Fu
- {
-
- Zi()
- {
- super("zhangsan",20); //相当于函数的重载
- //super("beijing");
-
- }
- }
- class Demo
- {
- public static void main(String[] args)
- {
- Zi zi = new Zi();
- zi.print();
- //zi.printAddress();
- }
- }
复制代码 |