码迷,mamicode.com
首页 > 编程语言 > 详细

Java继承与组合

时间:2016-08-09 12:13:30      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:


Java继承与组合


继承

java 中使用extends关键字表示继承关系,当创建一个类时,如果没有明确指出要继承的类,则是隐式地从根类Object进行继承。

  • 子类继承父类的成员变量
    1. 子类能够继承父类的public和protected成员变量,不能够继承父类的private成员变量
    2. 如果在子类中出现了同名称的成员变量,则子类的成员变量会屏蔽掉父类的同名成员变量。若要在子类中访问父类中同名成员变量,需要使用super关键字来进行引用。 
技术分享
public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Person p = new Student();
        System.out.println(p.name);
    }

}
class Person {
    public String name;
    public String sex;
    public short age;
    public Person(){
        name = "John";
        System.out.println("It is a person");
    }
}
class Student extends Person{
    public String name;
    public Student(){
        this.name = super.name;    //利用super引用父类变量
        System.out.println("It is a student");
    }
}

//输出
It is a person
It is a student
John
View Code 
  • 子类继承父类的方法
    1. 能够继承父类的public和protected成员方法,不能够继承父类的private成员方法
    2. 如果在子类中出现了同名称的成员方法,则称为覆盖,即子类的成员方法会覆盖掉父类的同名成员方法。如果要在子类中访问父类中同名成员方法,需要使用super关键字来进行引用
    3. 对于父类的静态方法,可以被继承但是不能被子类中的同名方法覆盖;子类中也可以隐藏父类的成员变量,只需要声明的变量名称相同,就算变量的数据类型不同,也一样可以隐藏父类成员变量。
技术分享
public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Person p = new Student();
        p.printName();
        p.printAge();
    }

}
class Person {
    private Pet p = new Pet();
    /*static{
        System.out.println("Person");
    }*/
    public String name;
    public String sex;
    public short age;
    public Person(String name){
        this.name = name;
    }
    public void printName(){
        System.out.println("My name is" + name);
    }
    public static void printAge(){
        System.out.println("This is used to print the people‘s age");
    }
}
class Student extends Person{
    public String name;
    public int age;
    public Student(){
        super("John");
        this.name = super.name;    //利用super引用父类变量
    }
    public static void printAge(){
        System.out.println("This is used to print the student‘s age");
    }
    public void printName(){
        System.out.println("It is used to print the student‘s name");
    }
}

//输出
It is used to print the student‘s name
This is used to print the people‘s age
View Code
  • 构造器
    1. 子类是不能够继承父类的构造器,但是,如果父类的构造器都是带有参数的,则子类的构造器中必须显示地通过super关键字调用父类的构造器并配以适当的参数列表。且必须是子类构造器的第一个语句。如果父类有无参构造器,则在子类的构造器中用super关键字调用父类构造器不是必须的,如果没有使用super关键字,系统会自动调用父类的无参构造器。
技术分享
class Person {
    public String name;
    public Person(String name){
        this.name = name;
    }
    public void printName(){
        System.out.println("My name is" + name);
    }
}
class Student extends Person{
    public Student(){
        super("John");
    }
    public void printName(){
        System.out.println("It is used to print the student‘s name");
    }
}
View Code

组合

组合是指在设计类的同时,把将要组合的类的对象加入到该类中作为自己的成员变量。

技术分享
class Student extends Person{
    public String name;
    public School school;        //组合
    public Student(){
        super("John");
        this.name = super.name;    //利用super引用父类变量
    }
    public void printName(){
        System.out.println("It is used to print the student‘s name");
    }
}
class Person {
    public String name;
    public Person(String name){
        this.name = name;
    }
    public void printName(){
        System.out.println("My name is" + name);
    }
}
class School {
    public String name;
    public String address;
    public void printName(){
        System.out.println("School name:" + name);
    }
    public void printAddress(){
        System.out.println("School address" + address);
    }
}
View Code

程序中选择集成或者组合的原则:

  • 如果存在一种IS-A的关系(比如Student是一个Person),并且一个类需要向另一个类暴露所有的方法接口,那么更应该用继承的机制。
  • 如果存在一种HAS-A的关系(比如Student必有一所School),那么更应该运用组合。 

Java继承与组合

标签:

原文地址:http://www.cnblogs.com/zhanglei93/p/5751280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!