A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© zzw777 中级黑马   /  2016-6-16 16:37  /  476 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class Demo1_Abstract {
        public static void main(String[] args) {
                //Animal a = new Animal();                        //错误:Animal是抽象的;无法实例化
                Animal a = new Cat();                                //父类引用指向子类对象
                a.eat();
        }
}
/*
B:抽象类特点
        a:抽象类和抽象方法必须用abstract关键字修饰
                abstract class 类名 {}
                public abstract void eat();
        b:抽象类不一定有抽象方法,有抽象方法的类一定是抽象类或者是接口
        c:抽象类不能实例化那么,抽象类如何实例化呢?
                按照多态的方式,由具体的子类实例化。其实这也是多态的一种,抽象类多态。
        d:抽象类的子类
                要么是抽象类
                要么重写抽象类中的所有抽象方法
*/
abstract class Animal {                                                //抽象类
        public abstract void eat();                                //抽象方法
}

class Cat extends Animal {
        public void eat() {
                System.out.println("猫吃鱼");
        }
}

class Demo1_Interface {
        public static void main(String[] args) {
                //Inter i = new Inter();                //接口不能被实例化,因为调用抽象方法没有意义
                Inter i = new Demo();                        //父类引用指向子类对象
                i.print();
        }
}
/*
A:接口概述
        从狭义的角度讲就是指java中的interface
        从广义的角度讲对外提供规则的都是接口
B:接口特点
        a:接口用关键字interface表示
                interface 接口名 {}
        b:类实现接口用implements表示
                class 类名 implements 接口名 {}
        c:接口不能实例化
                那么,接口如何实例化呢?
                按照多态的方式来实例化。
        d:接口的子类
                a:可以是抽象类。但是意义不大。
                b:可以是具体类。要重写接口中的所有抽象方法。(推荐方案)
C:案例演示
        接口特点
*/
interface Inter {
        public abstract void print();
}

class Demo implements Inter {
        public void print() {
                System.out.println("print");
        }
}

class Demo1_Polymorphic {
         public static void main(String[] args) {
                Cat c = new Cat();
                c.eat();

                Animal a = new Cat();                   //父类引用指向子类对象
                a.eat();
         }
}
/*
A:多态(polymorphic)概述
        事物存在的多种形态
B:多态前提
        a:要有继承关系。
        b:要有方法重写。
        c:要有父类引用指向子类对象。
C:案例演示
        代码体现多态
*/
class Animal {
        public void eat() {
                System.out.println("动物吃饭");
        }
}

class Cat extends Animal {
        public void eat() {
                System.out.println("猫吃鱼");
        }
}

class Demo2_Abstract {
        public static void main(String[] args) {
                System.out.println("Hello World!");
        }
}
/*
A:抽象类的成员特点
        a:成员变量:既可以是变量,也可以是常量。abstract是否可以修饰成员变量?不能修饰成员变量
        b:构造方法:有。
                用于子类访问父类数据的初始化。
        c:成员方法:既可以是抽象的,也可以是非抽象的。
B:案例演示
        抽象类的成员特点
C:抽象类的成员方法特性:
        a:抽象方法 强制要求子类做的事情。
        b:非抽象方法 子类继承的事情,提高代码复用性。
*/

abstract class Demo {
        int num1 = 10;
        final int num2 = 20;

        public Demo() {}

        public void print() {
                System.out.println("111");
        }

        public abstract void method();
}

class Test extends Demo {
        public void method() {
                System.out.println("111");
        }
}

class Demo2_Interface {
        public static void main(String[] args) {
                Demo d = new Demo();
                d.print();
                System.out.println(Inter.num);
        }
}
/*
成员变量;只能是常量,并且是静态的并公共的。
                  * 默认修饰符:public static final
                  * 建议:自己手动给出。
        构造方法:接口没有构造方法。
        成员方法:只能是抽象方法。
                  * 默认修饰符:public abstract
                  * 建议:自己手动给出。
*/
interface Inter {
        public static final int num = 10;
        //public Inter() {}                                        接口中没有构造方法

        /*public void print() {
       
        }*/

        public abstract void print();
}

class Demo /*extends object*/ implements Inter {        //一个类不写继承任何类,默认继承Object类
        public void print() {
                //num = 20;
                System.out.println(num);
        }

        public Demo() {
                super();
        }
}


class Demo2_Polymorphic {
        public static void main(String[] args) {
                Father f1 = new Son();
                System.out.println(f1.num);

                Son s = new Son();
                System.out.println(s.num);

                Father f2 = new Son();
                f2.print();

                Father f3 = new Son();
                f3.method();                          //相当于是Father.method()
        }
}

/*
成员变量
编译看左边(父亲),运行看左边(父类)

成员方法
编译看左边(父类),运行看右边(子类),动态绑定

静态方法
编译看左边(父类),运行看左边(父类)
(静态和类相关,算不上重写,所以,访问还是左边的)
只有非静态的成员方法,编译看左边,运行看右边
*/
class Father {
        int num = 10;

        public void print() {
                System.out.println("father");
        }

        public static void method() {
                System.out.println("father static method");
        }
}

class Son extends Father {
        int num = 20;

        public void print() {
                System.out.println("son");
        }

        public static void method() {
                System.out.println("son static method");
        }
}


class Demo3_SuperMan {
        public static void main(String[] args) {
                Person p = new SuperMan();         //父类引用指向子类对象,超人提升为了人
                                                                                        //父类引用指向子类对象就是向上转型
                System.out.println(p.name);         
                p.谈生意();
                //p.fly();

                SuperMan sm = (SuperMan)p;          //向下转型
                sm.fly();

                /*
                基本数据类型自动类型提升和强制类型转换
                */
                int i = 10;
                byte b = 20;
                //i = b;                                                        //自动类型提升
                //b = (byte)i;                                                //强制类型转换
        }
}

class Person {
        String name = "John";
        public void 谈生意() {
                System.out.println("谈生意");
        }
}

class SuperMan extends Person {
        String name = "surperMan";

        public void 谈生意() {
                System.out.println("谈几个亿的大单子");
        }

        public void fly() {
                System.out.println("飞出去救人");
        }
}


class Demo3_葵花宝典 {
        public static void main(String[] args) {
                岳不群 小岳子 = new 岳不群();
                小岳子.自宫();
        }
}

abstract class 葵花宝典 {
        public abstract void 自宫();
}

class 岳不群 extends 葵花宝典 {
        public void 自宫() {
                System.out.println("用牙签");
        }
}

class 林平之 extends 葵花宝典 {
        public void 自宫() {
                System.out.println("用指甲刀");
        }
}


class Demo4_Abstract {
        public static void main(String[] args) {
       
        }
}

/*
A.面试题1
        一个抽象类如果没有抽象方法,可不可以定义为抽象类?如果可以,有什么意义?
        可以
        这么做目的只有一个,就是不让其他类创建本类对象,交给子类完成
B.面试题2
        abstract不能和哪些关键字共存
        abstract和static
        被abstract修饰的方法没有方法体
        被static修饰的可以用类名.调用,但是类名.调用抽象方法是没有意义的
        abstract和final
        被abstract修饰的方法强制子类重写
        被final修饰的不让子类重写,所以他俩是矛盾
        abstract和private
        被abstract修饰的是为了让子类看到并强制重写
        被private修饰不让子类访问,所以他俩是矛盾的
*/

abstract class Demo {
        //public static abstract void print();                        //错误:非法的修饰符组合
        //public final abstract void print();                        //错误:非法的修饰符组合
        //private abstract void print();                                //错误:非法的修饰符组合
}


class Demo4_Animal {
        public static void main(String[] args) {
                //Cat c1 = new Cat();
                //c1.eat();
                method(new Cat());
                method(new Dog());

                //Animal a = new Cat();                                开发的时候很少在创建对象的时候用父类引用指向子类对象,直接创建子类对象更方便,可以使用子类中的特有属性和行为
        }

        //Cat c = new Dog();狗是一只猫,这是错误的
        /*public static void method(Cat c) {
                c.eat();
        }

        public static void method(Dog d) {
                d.eat();
        }*/

        //如果把狗强转成猫就会出现类型转换异常,ClassCastException
        public static void method(Animal a) {        //当做参数的时候用多态最好,因为扩展性强
                //关键字 instanceof 判断前边的引用是否是后边的数据类型
                if (a instanceof Cat) {
                        Cat c = (Cat)a;
                        c.eat();
                        c.catchMouse();
                }else if (a instanceof Dog) {
                        Dog d = (Dog)a;
                        d.eat();
                        d.lookHome();
                }else {
                        a.eat();
                }
               
        }
}

class Animal {
        public void eat() {
                System.out.println("动物吃饭");
        }
}

class Cat extends Animal {
        public void eat() {
                System.out.println("猫吃鱼");
        }

        public void catchMouse() {
                System.out.println("抓老鼠");
        }
}

class Dog extends Animal {
        public void eat() {
                System.out.println("吃狗肉");
        }

        public void lookHome() {
                System.out.println("看家");
        }
}


class Demo4_Animal {
        public static void main(String[] args) {
                //Cat c1 = new Cat();
                //c1.eat();
                method(new Cat());
                method(new Dog());

                //Animal a = new Cat();                                开发的时候很少在创建对象的时候用父类引用指向子类对象,直接创建子类对象更方便,可以使用子类中的特有属性和行为
        }

        //Cat c = new Dog();狗是一只猫,这是错误的
        /*public static void method(Cat c) {
                c.eat();
        }

        public static void method(Dog d) {
                d.eat();
        }*/

        //如果把狗强转成猫就会出现类型转换异常,ClassCastException
        public static void method(Animal a) {        //当做参数的时候用多态最好,因为扩展性强
                //关键字 instanceof 判断前边的引用是否是后边的数据类型
                if (a instanceof Cat) {
                        Cat c = (Cat)a;
                        c.eat();
                        c.catchMouse();
                }else if (a instanceof Dog) {
                        Dog d = (Dog)a;
                        d.eat();
                        d.lookHome();
                }else {
                        a.eat();
                }
               
        }
}

class Animal {
        public void eat() {
                System.out.println("动物吃饭");
        }
}

class Cat extends Animal {
        public void eat() {
                System.out.println("猫吃鱼");
        }

        public void catchMouse() {
                System.out.println("抓老鼠");
        }
}

class Dog extends Animal {
        public void eat() {
                System.out.println("吃狗肉");
        }

        public void lookHome() {
                System.out.println("看家");
        }
}


class Test2_Animal {
        public static void main(String[] args) {
                Cat c = new Cat("加菲",8);
                c.eat();
                c.sleep();

                JumpCat jc = new JumpCat("跳高猫",3);
                jc.eat();
                jc.sleep();
                jc.jump();
        }
}

/*
A:案例演示
动物类:姓名,年龄,吃饭,睡觉。
猫和狗
动物培训接口:跳高
*/

abstract class Animal {
        private String name;                                //姓名
        private int age;                                        //年龄

        public Animal() {}                                        //空参构造
         
        public Animal(String name,int age) {//有参构造
                this.name = name;
                this.age = age;
        }

        public void setName(String name) {
                this.name = name;
        }

        public String getName() {
                return name;
        }

        public void setAge(int age) {
                this.age = age;
        }

        public int getAge() {
                return age;
        }

        public abstract void eat();                        //吃饭

        public abstract void sleep();                //睡觉
}

interface Jumping {                                                //跳高的接口
        public void jump();
}

class Cat extends Animal {
        public Cat() {}                                        //空参构造

        public Cat(String name,int age) {//有参构造
                super(name,age);
        }

        public void eat() {
                System.out.println("猫吃鱼");
        }

        public void sleep() {
                System.out.println("侧着睡");
        }
}

class JumpCat extends Cat implements Jumping {
        public JumpCat() {}                                        //空参构造

        public JumpCat(String name,int age) {//有参构造
                super(name,age);
        }

        public void jump() {
                System.out.println("猫跳高");
        }
}

class Dog extends Animal {
        public Dog() {}                                        //空参构造

        public Dog(String name,int age) {//有参构造
                super(name,age);
        }

        public void eat() {
                System.out.println("吃肉");
        }

        public void sleep() {
                System.out.println("趴着睡");
        }
}


Demo2(2)_Polymorphic.png (201.41 KB, 下载次数: 4)

Demo2(2)_Polymorphic.png

Demo2_Polymorphic.png (201.25 KB, 下载次数: 5)

Demo2_Polymorphic.png

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马