2021年7月26日

讲师:王博

id10.8.159.18

今日内容:

·封装

·this关 键字

·多类合作练习题

·匿名对象

·继承

1封装

Jiava有三大特性:封装 继承 多态

封装也是java的一种编程思想

类的封装  包的封装  方法的封装  属性的封装

以下主要讲的是成员变量(属性)的封装

在写类的时候,之前的成员属性有修饰符修饰吗?谁都可以使用这个变量,只需要通过对象.属性就可以操作咱们的属性了。这样存在隐患。

可以加一个限制条件

1使用provider关键字进行修饰成员变量

2创建成员变量所对应的setget方法

3对象.set方法赋值  对象.get方法取值

package com.liujinghe.class1;

class PerSon{
    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        //设置年龄限制
        if(age>=0 && age<=130){
            this.age = age;
        }else{
            this.age=-1;
        }

    }
}
public class Demo2 {
    public static void main(String[] args) {
        //测试类
        PerSon perSon = new PerSon();
        perSon.setName("张三");
        System.out.println(perSon.getName());
        perSon.setAge(89);
        System.out.println(perSon.getAge());
    }
}

注意事项:

·对成员属性进行修饰

·写对应属性的getset方法

·从今天开始,进行类的标准化,以后所有的成员变量都要加private,并且下面写getset方法(java开发默认规矩)

·目的:以后在项目中可以规范咱们对对象的操作

·主要是可以隐藏一部分代码的逻辑

2this关键字

代表当前类对象

字面意思:这个

  1. 使用this可以表示当前的类对象。

可以使用this调用当前类的成员属性(常用)和成员方法

  1. this还可以调用构造方法

·只能在构造方法中调用另外一个构造方法

·this(带有实际参数)在构造方法中只能写在第一行

·不能调用自己的构造方法

package com.liujinghe.class2;
class Dog{
    private String name;
    private int age;
    private char sex;
    public Dog(){
        System.out.println("我是无参构造方法");
    }
    public Dog(String name){
        //使用this,不能自己调用自己
        //this("大黄");
        System.out.println("我是带有name参数的构造方法"+name);
    }
    public Dog(String name, int age, char sex) {
        //this调用的也是成员属性
//        this.name = name;
//        this.age = age;
//        this.sex = sex;
        //想再这个构造方法中去调用无参构造
//        this();
        this("李四");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        //this就是调用的成员属性name
        this.name = name;
    }

    public int getAge() {
        return age;
    }

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

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }
    public void eat(){
        //需要在eat方法调用sleep方法
        //使用this关键字可以调用成员方法
        this.sleep();
        System.out.println("狗在吃饭");
    }
    public void sleep(){
        System.out.println("狗睡觉的时候,千万不要下它");
    }

}
public class Demo {
    public static void main(String[] args) {
        Dog dog = new Dog("旺财",3,'');
        Dog dog1 = new Dog();
        dog.eat();
    }
}

3多类合作

  1. 人类

属性:name age sex cat

猫类

属性:name age

人这个类的一个成员变量时猫这个对象

3.1人类和猫类合作

package com.liujinghe.class2;

public class Demo2 {
    public static void main(String[] args) {
        Person person = new Person();
        Cat cat = new Cat();
        cat.setName("天猫");
        cat.setAge(3);
        person.setName("张三");
        person.setAge(19);
        person.setSex(true);
        person.setCat(cat);
        System.out.println(person.getCat().getName());
        System.out.println(person);
    }
}

package com.liujinghe.class2;

public class Person {
    private String name;
    private int age;
    private boolean sex;
    private Cat cat;//一个对象成另外一个类的属性

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public boolean isSex() {
        return sex;
    }

    public void setSex(boolean sex) {
        this.sex = sex;
    }

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                ", cat=" + cat +
                '}';
    }
}

package com.liujinghe.class2;

public class Cat {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

3.2班级类:

Name id student teacher

学生类:

教师类:

package com.liujinghe.class3;

class ClassRoom{
    private int id;
    private String name;
    private Student student;
    private Teacher teacher;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
}
class Student{
    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
class Teacher{
    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
public class Demo1 {
    public static void main(String[] args) {
        ClassRoom classRoom = new ClassRoom();
        Student student = new Student();
        Teacher teacher = new Teacher();
        teacher.setName("赵老师");
        teacher.setAge(35);
        student.setName("王五");
        student.setAge(18);
        classRoom.setId(10);
        classRoom.setName("java2019");
        classRoom.setStudent(student);
        classRoom.setTeacher(teacher);
        //无外乎就是赋值和取值的过程
        System.out.println(classRoom.getId());
        System.out.println(classRoom.getName());
        System.out.println(classRoom.getStudent().getName());
        System.out.println(classRoom.getStudent().getAge());
        System.out.println(classRoom.getTeacher().getName());
        System.out.println(classRoom.getTeacher().getAge());


    }
}

练习题

教师类

Name age student

学生类

Name age toy

玩具类

Kind name price

package com.liujinghe.class5;

class Teacher{
    private String name;
    private int age;
    private Student student;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }
}
class Student{
    private String name;
    private int age;
    private Toy toy;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public Toy getToy() {
        return toy;
    }

    public void setToy(Toy toy) {
        this.toy = toy;
    }
}
class Toy{
    private String kind;
    private String name;
    private double price;

    public String getKind() {
        return kind;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }

    public String getName() {
        return name;
    }

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

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}
public class Demo1 {
    public static void main(String[] args) {
        Teacher teacher = new Teacher();
        Student student = new Student();
        Toy toy = new Toy();
        toy.setKind("奥特曼");
        toy.setName("雷欧奥特曼");
        toy.setPrice(398);
        student.setName("小明");
        student.setAge(18);
        student.setToy(toy);
        teacher.setName("邢老师");
        teacher.setAge(78);
        teacher.setStudent(student);
        System.out.println(teacher.getName());
        System.out.println(teacher.getAge());
        System.out.println(teacher.getStudent().getName());
        System.out.println(teacher.getStudent().getAge());
        System.out.println(teacher.getStudent().getToy().getKind());
        System.out.println(teacher.getStudent().getToy().getName());
        System.out.println(teacher.getStudent().getToy().getPrice());
    }
}

方法的参数数类对象

汽车类:car

属性:颜色 品牌 车胎的个数

行为:飙车

修理厂类:Factory

属性:店的地址  店的名字 联系方式

行为:修车

Public void repair(Car car){}

package com.liujinghe.class6;
class Car{
    private String color;
    private String kind;
    private int wheelCount;//轮胎个数

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getKind() {
        return kind;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }

    public int getWheelCount() {
        return wheelCount;
    }

    public void setWheelCount(int wheelCount) {
        this.wheelCount = wheelCount;
    }
    //行为  飙车要求轮胎的个数必须为4个,如果少于四个必须送修理厂
    public void race(){
        if(wheelCount == 4){
            System.out.println("开着"+color+kind+",在航海中路以200km/h的时速飙车");
        }else{
            System.out.println("送修理厂。。。");
        }
    }
}
class Factory{
    private String name;
    private String address;//店的地址
    private String tel;//联系方式

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }
    //行为  修车
    //一个对象作为参赛传入方法,进行操作
    public void repair(Car car){
        //当轮胎的个数小于4的话,才修车
        //当轮胎的个数等于4的话,不修
        //传入car这个对象的时候,需要判断轮胎店个数
        if(car.getWheelCount() != 4){
            System.out.println("轮胎修理中......");
            car.setWheelCount(4);//修好以后的轮胎个数变为4
            System.out.println("修理完毕,请付款,5k");
        }else{
            System.out.println("车辆正常,不需修理");
        }
    }
}
public class Demo1 {
    public static void main(String[] args) {
        Car car = new Car();
        car.setKind("奥迪");
        car.setColor("黑色");
        car.setWheelCount(4);
        for (int i = 0; i < 10; i++) {
            car.race();
        }
        System.out.println("轮胎跑掉一个");
        car.setWheelCount(3);
        car.race();//送修理厂
        Factory factory = new Factory();
        factory.setName("航海中路修理厂(黑店)");
        factory.setAddress("航海中路60C12");
        factory.setTel("1384383838438");
        factory.repair(car);//修理成功
        //继续飙车
        for (int i = 0; i < 4; i++) {
            car.race();
        }
    }
}

电脑类

属性:键盘类对象 屏幕类对象

行为:写bug

键盘类:

属性:品牌 建的个数

行为:敲代码

屏幕类:

属性:品牌 尺寸

行为:显示bug

4匿名对象

Person person = new Person()

语法格式

类名 对象名 = new 类名();

赋值号的左侧只是在内存的战区定义的一个变量而已,引用数据

赋值号的右侧在内存的堆区创建了类的对象,可以进行赋值,真实的数据

匿名对象创建的格式

New 类名()

等号的左侧东西没了,但是是一次性的东西

package com.liujinghe.class8;

class Person{
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public void eat(){
        System.out.println("今天中午喝了西北风,晚上吃了土");
    }
}
public class Demo1 {
    public static void main(String[] args) {
        new Person().eat();
        new Person().setName("小明");
        String name = new Person().getName();
        System.out.println(name);//null
        Person person = new Person();
        person.setName("小红");
        String name1 = person.getName();
        System.out.println(name1);
        System.out.println(new Person());
        System.out.println(new Person());
        //匿名对象一般当做一个方法的参数来使用
        test(new Person());
        Person person1 = new Person();
        test(person1);
    }
    public static void test(Person person){
        person.eat();
    }
}

 

作业:

1今日代码至少3

2金山打字通

需求:

宠物饿了,需要铲屎官给宠物喂食

  1. 不同的宠物吃的食物不一样
  2. 不同的宠物恢复后体力值不一样
  3. 铲屎官和狗玩接飞盘游戏,体力值下降10,但是会与铲屎官 的亲密度加5
  4. 铲屎官和猫玩滚毛球,体力值减少12,但是会与铲屎官的亲密度加7

分析:

人类:猫类对象,狗类对象,方法,属性

狗类:方法

猫类:方法

需求:

定义两个类 Man Woman

Man 有个属性是老婆,有个方法是lol

Woman 有个属性是老公,有一个方法是shopping,还有一个方法生孩子,50%生男孩,50%生女孩

生孩子(先判断是否有老公,如果有老公,就可以生孩子)

如果生的是男孩,就调用打lol

如果生的是女孩,就调用shopping

 

posted @ 2021-07-26 19:01  张三疯321  阅读(44)  评论(0编辑  收藏  举报