Java - 面向对象高级 - 结业作业笔记

package com.tao.homework.OOP_high.homework01;

/**
 * Create By 刘鸿涛
 * 2021/12/30 20:12
 */
public class Homework01 {
    public static void main(String[] args) {
        Car c = new Car();  //price = 9 , color = red;
        Car c1 = new Car(100);  //c1.price = 100;
//      c =   9 red
//      c1 = 100.0  red
    }
}
class Car{
    double price = 10;
    static String color = "white";

    public String toString(){
        return price + "\t" + color;
    }
    public Car(){
        this.price = 9;
        this.color = "red";
    }
    public Car(double price){
        this.price = price;
    }
}

package com.tao.homework.OOP_high.homework02;

/**
 * Create By 刘鸿涛
 * 2021/12/30 20:28
 */
public class Homework02 {
    public static void main(String[] args) {
        System.out.println(Frock.getNextNum());
        System.out.println(Frock.getNextNum());

        Frock frock = new Frock();
        Frock frock1 = new Frock();
        Frock frock2 = new Frock();
        System.out.println(frock.serialNumer);
        System.out.println(frock1.serialNumer);
        System.out.println(frock2.serialNumer);
    }
}
class Frock{ //女装
    static int currentNum = 100000; //衣服出厂的序列号起始值
    int serialNumer;    //序列号属性
    public int getSerialNumer(){
        return getNextNum();
    }

    public static int getNextNum(){
        currentNum = currentNum + 100;
        return currentNum;
    }

    public Frock(){      //无参构造器中赋值序列号
        serialNumer = getNextNum();
    }

}


package com.tao.homework.OOP_high.homework03;

/**
 * Create By 刘鸿涛
 * 2021/12/30 20:50
 */
public class Test {
    public static void main(String[] args) {
        Animal cat = new Cat(); //编译类型Animal,运行类型 Cat
        Animal dog = new Dog();
        cat.shout();
        dog.shout();
        //输出为空
    }
}
abstract class Animal{
    abstract void shout();
}
class Cat extends Animal{
    @Override
    void shout() {
        System.out.println("猫会喵喵叫");
    }
}
class Dog extends Animal{
    @Override
    void shout() {
        System.out.println("狗会汪汪叫");
    }
}

package com.tao.homework.OOP_high.homework04;

/**
 * Create By 刘鸿涛
 * 2021/12/30 21:01
 */
public class Homework04 {
    public static void main(String[] args) {
        Cellphone cellphone = new Cellphone();
        cellphone.testWork();
    }
}
interface Computer{
    void work();        //接口中work方法,功能是运算
}
class Cellphone implements Computer{
    public void testWork(){
        work();
    }
    public void work(){
        System.out.println("手机正在工作...");
    }
}

package com.tao.homework.OOP_high.homework04;


/**
 * Create By 刘鸿涛
 * 2021/12/30 21:08
 */
public class Homework04_02 {
    public static void main(String[] args) {
        Cellphone02 cellphone02 = new Cellphone02();
        //解读
        //1.匿名内部类是
        /*
        new ICalculate() {
            @Override
            public double work(double n1, double n2) {
                return n1 + n2;
            }
        }
        同时也是一个对象
        他的编译类型 ICalculate,他的运行类型就是 匿名内部类
         */
        cellphone02.testWork(new ICalculate() {
            @Override
            public double work(double n1, double n2) {
                return n1 + n2;
            }
        },10,8);
        cellphone02.testWork(new ICalculate() {
            @Override
            public double work(double n1, double n2) {
                return n1 * n2;
            }
        },1,2);
    }
}
interface ICalculate{
    //work方法,是完成计算,但是题没有具体要求,所以自己设计
    //至于该方法完成怎样的计算,我们交给匿名内部类完成
    public double work(double n1,double n2);
}
class Cellphone02{
    //解读:当我们调用testWork方法时,直接传入一个实现ICalculate接口的匿名内部类即可
    //该匿名内部类,可以灵活地实现work,完成不同的计算任务
    public void testWork(ICalculate iCalculate,double n1, double n2){
        double result = iCalculate.work(n1,n2); //动态绑定
        System.out.println("计算后的结果是 = " + result);
    }

}
package com.tao.homework.OOP_high.homework05;

/**
 * Create By 刘鸿涛
 * 2021/12/30 21:39
 */
public class Homework05 {
    public static void main(String[] args) {
        new A().f1();   //匿名对象
    }
}
class A{
    private String NAME = "A类私有变量";
    public void f1(){
        class B{        //局部内部类(在方法里的类)
            private static final String NAME = "局部内部类常量";
            public void show(){

                System.out.println(NAME + "\n" +A.this.NAME);  //如果重名,用:外部类名.this.变量名
            }
        }
        B b = new B();
        b.show();
    }
}


package com.tao.homework.OOP_high.homework06;

/**
 * Create By 刘鸿涛
 * 2021/12/30 22:01
 */
public class Homework06 {
    public static void main(String[] args) {
        Person tang = new Person("唐僧", new Horse());    //默认交通工具为马
        tang.common();  //一般情况下用马
        tang.passRiver();   //过河情况用船

    }
}
interface Vehicles{ //交通工具接口
    void work();
}

class Horse implements Vehicles{
    @Override
    public void work(){
        System.out.println("一般情况 - 骑马");
    }

}
class Boat implements Vehicles{
    @Override
    public void work() {
        System.out.println("过河情况 - 划船");
    }
}

class VehiclesFactory{      //交通工具工厂类
    //创建交通工具工厂类,有两个方法分别获得交通工具 Horse 和 Boat
    //这里,我们将方法做成static
    public static Horse getHorse(){
        return new Horse();
    }
    public static Boat getBoat(){
        return new Boat();
    }

}
class Person{
    private String name;
    private Vehicles vehicles;       //交通工具

    //在创建人对象时,事先给他分配一个交通工具

    public Person(String name, Vehicles vehicles) {
        this.name = name;
        this.vehicles = vehicles;
    }

    //实例化Person对象"唐僧",要求一般情况下用Horse作为交通工具,遇到大河时用Boat作为交通工具
    //这里涉及到一个编程思路,就是可以把具体的要求,封装成方法 -> 这里就是编程思想
    //思考一个问题:如何不浪费,在构件对象时,传入的交通工具对象 -> 动脑筋
    public void passRiver(){
        //先得到船
//        if(vehicles == null){
        if(!(vehicles instanceof Boat)){
            vehicles = VehiclesFactory.getBoat();
        }
        vehicles.work();
    }
    public void common(){
        //得到马儿
        //判断一下,当前的 vehicles 属性是null,就获取一匹马
//        if(vehicles == null){
        if(!(vehicles instanceof Horse)){
            //多态:向上转型特点
            vehicles = VehiclesFactory.getHorse();
        }
        //这里体现使用接口调用
        vehicles.work();
    }



}

package com.tao.homework.OOP_high.homework07;

/**
 * Create By 刘鸿涛
 * 2021/12/31 11:01
 */
public class homework07 {
    public static void main(String[] args) {
        Car car = new Car(41);
        Car car1 = new Car(-1);
        Car car2 = new Car(20);
        car.getAir().flow();
        car1.getAir().flow();
        car2.getAir().flow();
    }
}
class Car{
    double temperature;     //温度
    public Car(double temperature){
        this.temperature = temperature;
    }
    //成员内部类
    class Air{               //空调类,内部类
        public void flow() {   //吹风功能
            if (temperature > 40) {
                System.out.println("吹冷气...");
            } else if (temperature < 0) {
                System.out.println("吹暖气...");
            } else {
                System.out.println("空调关闭");
            }
        }
    }

    //写个方法,为了创建成员内部类对象
//    public void f(){
//        Air air = new Air();
//        air.flow();
//    }
    public Air getAir(){
        return new Air();   //返回一个Air对象
    }
}


package com.tao.homework.OOP_high.homework08;


/**
 * Create By 刘鸿涛
 * 2021/12/31 11:32
 */
public class Homework08 {
    public static void main(String[] args) {
        //演示一下枚举值的switch使用
        Color green = Color.GREEN;      //这里更换颜色,此题目的目的知识学习switch语法,不必深究
        green.show();

        //比较一下
        //switch()中,放入枚举对象
        //在每个case后,直接写上在枚举类中,定义的枚举对象即可
        switch (green){
            case YELLOW:
                System.out.println("匹配到黄色");
                green.show();
                break;
            case BLACK:
                System.out.println("匹配到黑色");
                break;

                default:
                System.out.println("没有匹配到");
        }
    }
}
enum Color implements I{
    RED(255,0,0),BLUE(0,0,255),
    BLACK(0,0,0),YELLOW(255,255,0),
    GREEN(0,255,0);
    private int redValue;
    private int greenValue;
    private int blueValue;

    Color(int redValue, int greenValue, int blueValue) {
        this.redValue = redValue;
        this.greenValue = greenValue;
        this.blueValue = blueValue;
    }

    @Override
    public void show() {
        System.out.println("属性值为:" + redValue + "," + greenValue + "," + blueValue);
    }
}
//接口
interface I{
    void show();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鬼鬼骑士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值