Java内部类.继承.多态

2019-08-10  本文已影响0人  南在南方i

类里边包含:成员变量/构造方法/方法/代码块/内部类/继承/多态/组合

在创建一个对象之前需要做点事情

代码块

静态代码块

static {
        //age = 30; 静态代码块里面不能调用成员变量和实例方法
        System.out.println("静态代码块");
    }
public Person(){
        System.out.println("默认的无参构造方法 age="+age);
    }
    public Person(int age){
        this.age = age;
        System.out.println("有参构造方法");
    }

定义类

定义一个类,可以在多个地方定义

内部类

所谓内部类就是在一个类内部进行其他类结构的嵌套操作

内部类的优点

内部类的缺点

封装

面向对象方法的重要原则,就是把对象的属性和操作(或服务)结合为一个独立的整体,并尽可能隐藏对象的内部实现细节

继承

子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为

public void walk() {
        super.walk();
    }

多态polymorphic

1.同一个方法 多种实现=在不同的子类中有不同的实现
2.如果有继承关系:子类的对象可以使用父类变量接收 类型的自动向上转换


demo知识融合

package demo;

import java.util.ArrayList;

public class Person {
    String name;
    int age;
    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }

    public void walk(){

    }

    public void eat(){

    }
}

class civilServant extends Person{
    int salay;
    int count;
    public  civilServant(String name,int age,int salay,int count){
        super(name,age);
        this.salay = salay;
        this.count = count;
    }

    @Override
    public void walk() {
        System.out.println("慢慢走路");
    }

    @Override
    public void eat() {
        System.out.println("慢慢吃饭");
    }

    public void cShow() {
        System.out.println("civilServant{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salay=" + salay +
                ", count=" + count +
                '}');
    }
}

class officl extends Person{
    int salay;
    String tec;
    public officl(String name,int age,int salay,String tec){
        super(name,age);
        this.salay = salay;
        this.tec = tec;
    }

    @Override
    public void walk() {
        System.out.println("快快地走");
    }

    @Override
    public void eat() {
        System.out.println("快快吃饭");
    }

    public void oShow() {
        System.out.println("officl{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salay=" + salay +
                ", tec='" + tec + '\'' +
                '}');
    }
}

class Test{
    public static void main(String[] args){
        ArrayList<Person> peoples = new ArrayList();
        Person c1 = new civilServant("张三",25,13000,45);
        Person c2 = new civilServant("李四",21,15000,24);
        Person c3 = new civilServant("王五",30,14400,30);
        Person c4 = new civilServant("FCF",26,10540,42);
        peoples.add(c1);
        peoples.add(c2);
        peoples.add(c3);
        peoples.add(c4);

        Person o1 = new officl("渣渣辉",19,5000,"自动加血");
        Person o2 = new officl("古天乐",28,6800,"开局满蓝");
        Person o3 = new officl("林子聪",20,9500,"双倍经验");
        peoples.add(o1);
        peoples.add(o2);
        peoples.add(o3);

        for (Person p : peoples){
            if (p instanceof civilServant){
                civilServant c = (civilServant)p;
                c.cShow();
                c.walk();
                c.eat();
            }else {
                officl o =(officl)p;
                o.oShow();
                o.walk();
                o.eat();
            }
        }
    }
}

PS.

今天最开始单独做了一个demo,刚开始有一种无从下手的感觉,才发现自己的问题。以前上课听的时候自我感觉良好,殊不知什么都只是学到了皮毛而已。经过讲解之后才找到了一丝熟悉的感觉,希望自己能踏踏实实做个人吧

上一篇 下一篇

猜你喜欢

热点阅读