Java学习(6)—— 面向对象2(对象的基本特征)

 面向对象的基本特征:封装、继承、多态

一.   封装

        (1)将类的属性私有化(private),提供公共(public)的方法来设置(set)和获取(get)该属性的值

        (2)将类的方法私有化(private)

public class PersonTest {
    public static void main(String[] args){
        Person person = new Person();

        //person.age = 22;                 错误:声明为private的属性,不能直接调用
        //System.out.println(person.age);

        person.setAge(22);
        System.out.println(person.getAge()); //22

        person.name = "Tom";
        //person.myName();                错误:声明为private的方法,不能直接调用
        person.introduction();            //输出:   姓名:Tom
                                          //        年龄:22
    }
}

class Person{
    String name;
    private int age;

    //提供关于age的get和set方法(可以使用IDEA中的快捷键:鼠标选中私有属性,然后ALT+ENTER)      
    public void setAge(int age){
        this.age = age;
    }

    public int getAge() {
        return this.age;
    }

    private void myName(){
        System.out.println("姓名:" + name);
    }

    public void introduction(){
        myName();
        System.out.println("年龄:" + age);
    }

}

二.   继承

       class A extends B {  }  :A为子类,B为父类,子类A继承了父类B,A可以获取B中声明的所有属性和方法(包括父类中声明为private的结构,但是由于封装性的影响,子类不可以直接调用)。

       特点:(1)一个类可以有多个子类;(2)一个类只能有一个父类;(3)如果B继承了A,C继承了B,则C拥有了A和B的所有属性和方法;(4)如果我们没有显式的声明一个类的父类,则该类的父类默认为Object类,因此所有的类都直接或间接地继承了Object类。

public class Test {
    public static void main(String[] args) {
        Creature creature = new Creature();
        creature.live();      //输出:拥有生命

        Person person = new Person();
        person.live();        //输出:拥有生命  子类Person可以调用父类Creature的方法

        Man man = new Man();
        man.live();           //输出:拥有生命  子类Man继承了Person类,Person类继承了Creature类,则Man可以调用Person和Creature的属性和方法
        man.eat();            //输出:吃饭     子类Man可以调用父类Person的方法
        man.name = "Tom";     //子类Man可以调用父类Person的属性
        //man.age = 22;       //由于age在父类Person中声明为private,子类Man虽然继承了该属性,但是由于封装性的原因不可以直接调用
        man.setAge(22);
        //man.myName();      //由于myName()在父类Person中声明为private,子类Man虽然继承了该方法,但是由于封装性的原因不可以直接调用
        man.introduction();  
        man.sex = '男';      //子类Man调用其特有的属性
        man.appearance();   //输出:handsome       子类Man调用其特有的方法


    }
}

class Creature{
    public void live(){
        System.out.println("拥有生命");
    }
}

class Person extends Creature{           //Person继承了Creature
    String name;
    private int age;

    //提供关于age的get和set方法
    public void setAge(int age){
        this.age = age;
    }

    public int getAge() {
        return this.age;
    }

    private void myName(){
        System.out.println("姓名:" + name);
    }

    public void introduction(){
        myName();
        System.out.println("年龄:" + age);
    }

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

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

}


class Man extends Person{               //Man继承了Person
    char sex;    //子类可以定义属于自己的属性和方法

    public void appearance(){
        System.out.println("handsome");
    }

}

       方法的重写

       方法的重写:子类可以根据需要对从父类继承过来的方法进行重写,在执行时,子类的方法将覆盖父类的方法。

     (1)子类重写的方法必须与父类被重写的方法具有相同的方法名称、参数列表;

     (2)如果父类被重写的方法的返回值类型是void,则子类重写的方法的返回值类型只能是void;如果父类被重写的方法的返回值类型是基本数据类型,则子类重写的方法的返回值类型必须是相同的基本数据类型;

     (3)子类重写的方法的访问权限不能小于父类被重写的方法的访问权限;子类不可以重写父类中声明为private的方法。

       通常子类在对方法重写时,与父类中被重写的方法的声明完全一样。

       注意:子类和父类中的同名同参数的方法要么都声明为非static(考虑重写),要么都声明为static(不是重写)。

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

        Person person = new Person();
        person.eat();        //输出:吃饭
        person.behaviour();  //输出:吃饭
                             //     睡觉

        Man man = new Man();
        man.eat();          //输出:男生吃饭较多     由于子类对父类的eat进行重写,所以调用的是子类的重写后的eat方法

        man.behaviour();    //输出:男生吃饭较多    //由于子类对父类的eat进行重写,所以调用的是子类的重写后的eat方法
                            //     睡觉          //由于父类的sleep是private,因此子类不可以对其进行重写,所以子类中的eat方法不是对父类的重写,而是自己重新定义的新方法
    }
}


class Person {           //Person继承了Creature
    String name;
    int age;

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

    private void sleep(){
        System.out.println("睡觉");
    }

    public void behaviour(){
        eat();
        sleep();
    }

}


class Man extends Person{
    char sex;

    public void appearance(){
        System.out.println("handsome");
    }

    //对父类eat进行重写
    public void eat(){
        System.out.println("男生吃饭较多");
    }

    //不是对父类sleep的重写,相当于是子类Man重新定义的一个方法
    public void sleep() {
        System.out.println("需要多睡觉");
    }

}

       super关键字

       super调用属性或方法

     (1)可以在子类中使用 " super.属性 " 或 " super.方法 " 的方式显式的调用父类中声明的属性或方法,通常可以省去super;

     (2)当子类和父类中定义的属性同名时,想在子类中调用父类中声明的属性,必须使用 " super.属性 " ;

     (3)当子类重写了父类的方法后,想在子类中调用父类中被重写的方法时,必须使用 " super.方法 " 。

public class Test {
    public static void main(String[] args) {
        Man man = new Man();
        man.show();
    }
}

class Creature{
    public void live(){
        System.out.println("拥有生命");
    }

}

class Person extends Creature{
    String name;
    int age;
    char sex = '空';

    public void eat(){
        super.live();
        System.out.println("吃饭");
    }

    private void sleep(){
        System.out.println("睡觉");
    }

}


class Man extends Person{
    char sex = '男';

    //对父类eat进行重写
    public void eat(){
        System.out.println("男生吃饭较多");
    }

    public void show(){
        System.out.println(this.sex);    //输出:男
        System.out.println(super.sex);   //输出:空
        this.eat();                      //输出:男生吃饭较多
        super.eat();                     //输出:拥有生命
                                         //     吃饭
    }

}

       super调用构造器

     (1)可以在子类的构造器中使用 " super(形参列表) " 的方式,调用父类中声明的指定构造器;

     (2)" super(形参列表) " 的使用,必须声明在子类构造器的首行;

     (3)在类的构造器中," this(形参列表) " 和 " super(形参列表) "只能二选一。

     (4)当我们在构造器首行没有声明 " this(形参列表) " 和 " super(形参列表) ",则默认调用父类中空参的构造器 " super() "。

public class Test {
    public static void main(String[] args) {
        Man man = new Man('男');       //输出: 所有生物都有生命
                                       //       姓名:Tom, 年龄:22
                                       //       性别:男

        Person person = new Person("Bob");   //输出: 所有生物都有生命
                                             //       姓名:Bob
                                    
    }
}

class Creature{

    public Creature(){
        System.out.println("所有生物都有生命");
    }
}

class Person extends Creature{
    String name;
    int age;

    public Person(){

    }

    public Person(String name){
        System.out.println("姓名:" + name);
    }

    public Person(String name, int age){
        super();
        System.out.println("姓名:" + name + ", 年龄:" + age);
    }
}


class Man extends Person{
    char sex;

    public Man(){

    }

    public Man(char sex){
        super("Tom",22);
        System.out.println("性别:" + sex);
    }
}

三.   多态

        对象的多态性:父类的引用指向子类的对象(子类的对象赋给父类的引用),形式为: 父类名  变量 = new 子类名();

        多态的使用(虚拟方法调用):有了对象的多态性以后,只能调用父类中声明的方法,但实际执行的是子类重写父类的方法。父类的方法称为虚拟方法,父类根据赋给他的不同子类对象,动态调用子类的该方法。

        多态性的使用前提:(1)必须有继承关系;(2)要有方法的重写

        对象的多态性只适用于方法,不适用于属性(结果还是父类的)

public class Test {
    public static void main(String[] args) {
        Test test = new Test();

        Person person = new Man();  //对象的多态性:父类的引用指向子类的对象,形式为:父类名 变量 = new 子类名();

        person.eat();  //多态的使用:当调用子类和父类同名同参数的方法时,实际执行的是子类重写父类的方法 ———— 虚拟方法调用
        //输出:吃的较多

        //person.manAppearance();  错误:只能调用父类的方法

        System.out.println(person.sex);  //输出:无   多态性只适用于方法,不适用于属性,因此结果还是父类的"无",而不是子类的"男"。

        test.show(new Man());      //吃的较多
        test.show(new Woman());    //吃的较少
    }
    
    public void show(Person person){
        person.eat();
    }
}



class Person {
    String name;
    int age;
    char sex = '无';

    public void introduction(){
        System.out.println("年龄:" + age);
    }

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

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

}


class Man extends Person{
    char sex = '男';

    public void eat(){
        System.out.println("吃的较多");
    }

    public void manAppearance(){
        System.out.println("handsome");
    }

}


class Woman extends Person{
    char sex = '女';

    public void eat(){
        System.out.println("吃的较少");
    }

    public void womanAppearance(){
        System.out.println("beautiful");
    }

}

       向下转型

       想要调用子类的属性和方法,需要向下转型(使用强制类型转换符)

       为了避免在向下转型时出现错误,先使用instanceof进行判断,

       a instanceof A:判断对象a是否是类A的实列,如果是,返回true,如果不是,返回false。

       当B是A的父类,如果a instanceof A 返回true,则a instanceof B 也返回true。

public class Test {
    public static void main(String[] args) {
        //对象的多态性:父类的引用指向子类的对象,形式为:父类名 变量 = new 子类名()
        Person person = new Man();
        //多态的使用:当调用子类和父类同名同参数的方法时,实际执行的是子类重写父类的方法 ———— 虚拟方法调用
        person.eat();  //输出:吃的较多

        //person.manAppearance();  错误:只能调用父类的方法,不能调用子类的方法
        System.out.println(person.sex);  //输出:无   多态性只适用于方法,不适用于属性,因此结果还是父类的"无",而不是子类的"男"。


        //想要调用子类的属性和方法,需要向下转型(使用强制类型转换符)
        Man man = (Man)person;
        man.manAppearance();          //handsome
        System.out.println(man.sex);  //男

        person.sleep();   //睡觉

        //Woman woman = (Woman)person;   //错误,由于Person person = new Man(),不能再转为Woman



        //为了避免在向下转型时出现错误,先使用instanceof进行判断
        Person person1 = new Woman();
        if (person1 instanceof Woman){
            Woman woman = (Woman)person1;
            woman.womanAppearance();        //beautiful
            System.out.println(woman.sex);  //女
        }else if(person1 instanceof Man){
            Man man1 = new Man();
            man1.manAppearance();
            System.out.println(man1.sex);
        }


        //Person person2 = new Person();
        //Man man2 = (Man)person2;     错误

        //向上转型
        Object o = new Man();
        Person p = (Person)o;
        p.eat();   //吃的较多
        System.out.println(p.sex);  //无
    }

}


class Person {
    String name;
    int age;
    char sex = '无';

    public void introduction(){
        System.out.println("年龄:" + age);
    }

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

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


class Man extends Person{
    char sex = '男';

    public void eat(){
        System.out.println("吃的较多");
    }

    public void manAppearance(){
        System.out.println("handsome");
    }
}


class Woman extends Person{
    char sex = '女';

    public void eat(){
        System.out.println("吃的较少");
    }

    public void womanAppearance(){
        System.out.println("beautiful");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值