Java学习DAY6

第六章 面向对象(上)

1.面向对象思想

在这里插入图片描述

2.类和对象

类:一组相关的属性和行为的集合,java语言最基本的组成单位就是类。
对象:该类事物的具体体现。
举例: 类 学生
   对象 张三
在这里插入图片描述
定义一个学生类:

public class Student {
    String name;
    int age;
    public void study(){
        System.out.println("好好学习,天天向上。");

    }
    public  void qiaFan(){
        System.out.println("学生饿了得吃饭。");
    }
}

3.类的使用

在这里插入图片描述

public class keyword {
    public static  void main(String[] args) {
        Student s = new Student();//new出来的东西都放堆里面,堆内存的数据有默认值。

        System.out.println("name:" + s.name);
        System.out.println("age:" + s.age);
        System.out.println("-----------------------");
        s.name = "Lily";
        s.age = 30;
        System.out.println("name:" + s.name);
        System.out.println("age:" + s.age);
        System.out.println("-----------------------");
        s.study();
        s.qiaFan();
    }
}

结果:

name:null
age:0
-----------------------
name:Lily
age:30
-----------------------
好好学习,天天向上。
学生饿了得吃饭。

Process finished with exit code 0

4.内存分布

一个对象的内存分布:
在这里插入图片描述
两个对象的内存分布:方法共用
在这里插入图片描述
两个对象的内存分布:两个引用指向同一个内存分布
在这里插入图片描述

5.成员变量和局部变量

在这里插入图片描述

6.private关键字

在这里插入图片描述
定义学生类,私有化age:

public class Student {
    String name;
    private int age;
    public void setAge(int a){
        if(a<0||a>100){
            System.out.println("你给的年龄有误");
        }else{
            age=a;
        }

    }
    public int getAge(int a){
        return age;
    }
    public void show(){
        System.out.println("age:"+age);
    }
   
}

在main方法里调用:

public class keyword {
    public static  void main(String[] args) {
        Student s = new Student();//new出来的东西都放堆里面,堆内存的数据有默认值。
        System.out.println("name:" + s.name);
        System.out.println("-----------------------");

        s.name = "Lily";
        s.setAge(30);
        System.out.println("name:" + s.name);
        s.show();


    }
}

结果:

name:null
-----------------------
name:Lily
age:30

Process finished with exit code 0

7.封装

在这里插入图片描述

8.this关键字

在这里插入图片描述
在这里插入图片描述
注意看,代码setAge(int age)方法中this.age拿的是private int age,其他的age拿的是括号里的int age ——就近原则。

public class Student {
    String name;
    private int age;
    public void setAge(int age){
        if(age<0||age>100){
            System.out.println("你给的年龄有误");
        }else{
            this.age=age;
        }

    }
    public int getAge(int a){
        return age;
    }
    public void show(){
        System.out.println("age:"+age);
    }

}

9.构造方法

在这里插入图片描述

10.标准类的写法

学生类:

public class Student {
    private String name;
    private int age;
    //构造方法
    public Student(){}
    public Student(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){
        if(age<0||age>100){
            System.out.println("你给的年龄有误");
        }else{
            this.age=age;
        }

    }
    public int getAge(){
        return age;
    }


}

测试:

public class keyword {
    public static  void main(String[] args) {
        //无参构造方法+setXX()
        Student s = new Student();
        System.out.println("-----------------------");

        s.setName("Lily");
        s.setAge(30);
        System.out.println(s.getName()+"----"+s.getAge());

        //带参构造方法
        Student s2=new Student("Lily",30);
        System.out.println(s.getName()+"----"+s.getAge());

    }
}

结果:

-----------------------
Lily----30
Lily----30

Process finished with exit code 0

11.继承的特点

在这里插入图片描述
在这里插入图片描述
Java中类不支持多继承,但支持多层继承

/*
public class Son extends GrandFather{
    public static  void main(String[] args) {
        Son s=new Son();
        s.grandfathersay();
    }

}
*/
public class Son extends Father{
    public static  void main(String[] args) {
        Son s=new Son();
        s.fathersay();
    }

}
/*
public class Son extends Father,GrandFather{
    //错误,java类只支持单继承,不支持多继承,如果son想同时拥有father和grandfather中的方法,可以
    //让father继承grandfather类,再让son继承father类即可。
}
*/

在这里插入图片描述

---------------------------------
public class GrandFather {
   public  int age=70;

}
----------------------------------
public class Father extends GrandFather {
    public int age=45;
    public void printAge(){
        int age=10;
        System.out.print(age);//10
        //System.out.print(this.age);//45
        //System.out.print(super.age);//70
    }                                                           << —————|
 }                                                                      |
------------------------------------                                    |
public class Son extends Father{                                        |
    public static  void main(String[] args) {                           |
        Son s=new Son();                                                |
        s.printAge();//此时输出的age是10;如果想输出45或者70,应该怎样做————|(看箭头)
    }

}
super和this的用法

在这里插入图片描述

继承中构造方法和成员方法的特点

在这里插入图片描述

----------------------------------------------
public class GrandFather {

   public  GrandFather(){
       System.out.println("爷爷的无参构造方法");
   }
   public GrandFather(String name){
       System.out.println("爷爷的带参构造方法");
       System.out.println(name);
   }
}
----------------------------------------------
public class Father extends GrandFather {
    public  Father(){
        System.out.println("爸爸的无参构造方法");
    }
    public Father(String name){
        System.out.println("爸爸的带参构造方法");
        System.out.println(name);
    }
 }
 ----------------------------------------------------
 public class Son {
    public static  void main(String[] args) {
        Father s=new Father();
        System.out.println("---------------");
        Father s2=new Father("Bob");
    }

}

结果;

爷爷的无参构造方法
爸爸的无参构造方法
---------------
爷爷的无参构造方法
爸爸的带参构造方法
Bob

Process finished with exit code 0

在这里插入图片描述

方法重写

在这里插入图片描述
在这里插入图片描述
访问权限由低到高:private——默认修饰符——public

--------------------------------------------------
public class GrandFather {

   public void show(){
       System.out.println("爷爷");
   }

}
---------------------------------------------------
public class Father extends GrandFather {
    @Override
    public void show(){
        System.out.println("爸爸");
    }

 }
---------------------------------------------------

继承的练习:

---------------------------------------------------------
public class Human {
    private int age;
    private String name;
    public Human(){}
    public Human(int age,String name){
        this.age=age;
        this.name=name;

    }

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

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

}
-----------------------------------------------------
public class Student extends Human {
    public Student(){}
    public Student(int age,String name){
        super(age,name);
    }

    public void study(){
        System.out.println("学生要好好学习");
    }

 }
 -------------------------------------------------------
public class Test {
    public static  void main(String[] args) {
        Student s1=new Student();
        s1.setAge(18);
        s1.setName("Lily");
        System.out.println(s1.getName()+"----"+s1.getAge());
        s1.study();
        System.out.println("---------------------------------");


        Student s2=new  Student(17,"Bob");
        System.out.println(s2.getName()+"----"+s2.getAge());
        s2.study();
    }

}

结果:

Lily----18
学生要好好学习
---------------------------------
Bob----17
学生要好好学习

Process finished with exit code 0

12.多态

在这里插入图片描述
在这里插入图片描述

public class Animal {
    public int age=40;
    public void eat(){
        System.out.println("吃东西");

    }
}

public class Cat extends Animal{
    public int age=20;
    public int weight =30;
    public void eat(){
        System.out.println("猫吃鱼");

    }
    public void playGame(){
        System.out.println("猫要玩游戏");
    }
}



public class Test {
    public static  void main(String[] args) {
        /*左            右*/
        Animal a = new Cat();
        System.out.println(a.age);//40
        //System.out.println(a.weight);//报错
        a.eat();//猫吃鱼
        //a.playGame();//报错
    }

}


在这里插入图片描述
多态的转型(接上例):
在这里插入图片描述

public class Test {
    public static  void main(String[] args) {
        /*左            右*/
        Animal a = new Cat();//向上转型
        a.eat();//猫吃鱼
        
        Cat c = (Cat)a;//向下转型
        c.eat();//猫吃鱼
        c.playGame();//猫要玩游戏

        a=new Dog();//向上转型
        a.eat();//狗吃骨头


        Cat cc=(Cat) a;//异常,不能从Dog子类变成Cat子类
        cc.eat();
        cc.playGame();
    
    }

}

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值