黑马程序员技术交流社区

标题: 继承 [打印本页]

作者: Over_Mstuang    时间: 2015-8-6 22:55
标题: 继承
/*
        继承的好处:
                A:提高了代码的复用性。
                B:让类与类之间产生了一个关系,是多态的前提。

        继承的特点:
                A:Java只支持单继承,不支持多继承。
                        为什么呢?因为如何可以多继承,就会出现调用不明确的问题。
                B:Java支持多层(重)继承(继承体系)

        什么时候把类的关系定义为继承呢?
                由于继承体现了一种关系:is a的关系。xxx is yyy的一种。
                以后,你在定义类(A,B)的时候:
                        如果他们有关系:A is a B 的一种。或者B is a A。
                        那么,他们之间就存在继承关系。前者A是子类,后者B是子类。

                注意:不要为了获取部分功能,而去使用继承。
*/
class Person
{
        public void show()
        {
                System.out.println("person");
        }
}

/*
class School
{
        public void show()
        {
                System.out.println("school");
        }
}
*/

//class Student extends Person,School
class Student extends Person
{
        public void method()
        {
                System.out.println("method");
        }
}

class Coder extends Student
{

}

class ExtendsDemo2
{
        public static void main(String[] args)
        {
                Person p = new Person();
                p.show();
                System.out.println("------");

                Student s = new Student();
                s.show();
                s.method();
                System.out.println("------");

                Coder c = new Coder();
                c.show();
                c.method();
        }
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2