day13_oop

今日内容

零、 复习昨日
一、final
二、static
三、多态
四、向上转型&向下转型
五、多态应用

零、 复习昨日

  • 封装
    • 类的封装: 1 属性私有 2提供setget
  • 继承
    • A extends B
    • 子类可以使用父类非私有属性和方法
    • 好处: 复用,多态准备
  • 重写/覆写/覆盖/Override
    • 子类重写父类的方法,以适应自己本身
    • 多态准备
  • this super
    • 发现: 在创建子类对象时先创建父类对象
    • 为什么? 默认构造方法第一行有个super()调用父类构造方法
    • super代表父类对象 this 当前对象
    • super() super(实参) super.属性 super.方法
    • this() this(实参) this.属性 this.方法
  • 访问修饰符
    • public > protected > package > private
  • 子父类创建对象的过程
    • new 子类(),但是先super()调用父类构造
    • 父类属性先初始化,后再执行父类构造方法内代码
    • 在子类属性初始化,最后执行子类构造方法内代码
    • 将对象地址赋值给引用

一、final

final 最终最后,修饰符,用来修饰类,属性,方法

final 修饰类,该类不能被继承,即该类不能有子类 (太监类)

image-20230412091650500

final 修饰方法,该方法不能被重写

image-20230412092003468

final 修饰属性/变量,变量变常量,不能被更改

image-20230412092459685

    final int PEOPLE_AGE = 19;
    /**
     * 命名规范:
     *  变量命名驼峰原则: userName
     *  常量命名: 全部大写,多个单词之间使用下划线
     */
public static void main(String[] args) {
        // final 限制了father引用不能再改变
        final Father father = new Father( );
        // 但是不影响对象内部属性的变化
        father.age2 = 19;

        // 即不能像这样,再次引用别的对象
        // father = new Father();
    }

二、static

2.1 特点

static 静态修饰符,修饰属性 ,方法 ,类(静态内部类)

package com.qf.static_;

/**
 * --- 天道酬勤 ---
 *
 * @author QiuShiju
 * @desc
 */
public class Huangniu {
    /**
     * static 修饰的属性在内存只有一份,被该类所有对象共享
     * 即这些静态属性叫做: 类属性,属于类
     * --------------------------------------
     * - static修饰属性和方法,随着类(字节码文件)加载而初始化
     * - static修饰的属性和方法,可以直接通过类名调用,不用对象
     * - static修饰的属性和方法,在内存只有一份,不属于某个对象,而是属于字节码文件
     */
    static int ticketNum = 100;// 许嵩演唱会票

    /**
     * 普通成员变量,即成员属性,每次创建对象,每个对象都有一份属于自己的属性
     * 即这些属性叫做: 对象属性,这些属性属于对象
     */
    String name;


    // 售票
    public void sell(){
        ticketNum--;
        System.out.println("售出一张" );
    }

    // 查余票
    public int getTicketNum(){
        return ticketNum;
    }

    public static void jiao(){
        System.out.println("黄牛叫卖" );
    }

    public static void main(String[] args) {
        Huangniu niu1 = new Huangniu( );// 黄牛
        System.out.println("1售票前"+niu1.getTicketNum( ));
        niu1.sell();// 售票
        System.out.println("1售票后"+niu1.getTicketNum( ));
        niu1.name = "牛1";

        System.out.println("----------" );

        Huangniu niu2 = new Huangniu( );// 黄牛
        System.out.println("2售票前"+niu2.getTicketNum( ));
        niu2.sell();// 售票
        System.out.println("2售票后"+niu2.getTicketNum( ));
        System.out.println(niu2.name );


        System.out.println(Huangniu.ticketNum );
        Huangniu huangniu = new Huangniu( );
    }
}

image-20230412101854393

总结:

  • static修饰属性和方法,随着类(字节码文件)加载而初始化
  • static修饰的属性和方法,可以直接通过类名调用,不用对象
  • static修饰的属性和方法,在内存只有一份,不属于某个对象,而是属于字节码文件
  • 静态方法只能调用静态
  • 静态方法内部不能使用this

2.2 应用

1 当某些数据需要被所有对象共享时,给数据(属性或变量)设置static,例如票

2 当某些方法需要方便调用时,可以将方法设置静态,这样就是通过类名直接调用,而不用创建对象

 		int[] arr = {1,23,4,5,6};
        System.out.println(Arrays.toString( arr) );
        System.out.println(Math.abs(-1));

总结: 有静态修饰就直接使用,没有静态修饰,就创建对象用对象调用

三、多态

生活中的多态: 变形金刚,蝌蚪,铁甲小宝

程序中的多态: 同一个方法,出现不同的结果

多态的出现,要满足三个前提条件:

  • 继承/实现
  • 重写
  • 父类引用指向子类对象(向上转型)

演示

package com.qf.poly;

/**
 * --- 天道酬勤 ---
 *
 * @author QiuShiju
 * @desc
 */
public class TestPoly {

    public static void main(String[] args) {
        // 创建对象
        // 类名 对象名 = new 类名();
        // 类名 引用 = new 构造方法();
        Dog dog = new Dog();

        // 父类引用,指向子类对象(向上转型)
        // Animal animal = new Dog();
        //
        // animal.eat();
        feed(new Dog( ));
        feed(new Cat( ));
        feed(new Pig( ));
        feed(new Tiger( ));

    }

    // 多态改造
    // Animal animal = new Dog( );
    // Animal animal = new Cat( );
    // Animal animal = new Pig( );
    // Animal animal = new Tiger( );
    public static void feed(Animal animal) {
        animal.eat();// 此方法体现出多态效果
        /**
         * 编译看父类
         * 运行看子类
         */
    }


    // 动物园饲养动物
    // public static void feed(Dog dog){// Dog dog = new Dog
    //     dog.eat();
    // }
    //
    // public static void feed(Cat cat) {
    //     cat.eat();
    // }
    // public static void feed(Pig pig) {
    //     pig.eat();
    // }
}

多态应用场景:

  • 方法的参数列表是父类引用
  • 定义一个父类类型数组,存储所有动物,创建Animal[]

多态的好处:

  • 扩展性
  • 解耦合/松耦合
    • 开发原则: 高内聚 低耦合
      {
      // cat.eat();
      // }
      // public static void feed(Pig pig) {
      // pig.eat();
      // }
      }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用C++实现的oop租车系统(多重继承)的代码示例: ```cpp #include <iostream> #include <string> #include <vector> using namespace std; // 定义车辆类 class Vehicle { public: Vehicle(const string& make, const string& model, int year, int rent_per_day) : make_(make), model_(model), year_(year), rent_per_day_(rent_per_day) {} // 打印车辆信息 void print_info() const { cout << make_ << " " << model_ << " (" << year_ << ") - $" << rent_per_day_ << "/day" << endl; } private: string make_; // 品牌 string model_; // 型号 int year_; // 年份 int rent_per_day_; // 租金/天 }; // 定义客户类 class Customer { public: Customer(const string& name, int age) : name_(name), age_(age) {} // 打印客户信息 void print_info() const { cout << name_ << " (" << age_ << " years old)" << endl; if (rented_vehicle_) { cout << "Rented vehicle: "; rented_vehicle_->print_info(); } } // 租用车辆 void rent_vehicle(Vehicle* vehicle) { rented_vehicle_ = vehicle; cout << name_ << " rented "; rented_vehicle_->print_info(); } private: string name_; // 姓名 int age_; // 年龄 Vehicle* rented_vehicle_ = nullptr; // 租用的车辆 }; // 定义租车公司类 class CarRentalCompany { public: // 初始化车辆和客户 CarRentalCompany() { // 添加车辆 vehicles_.push_back(new Vehicle("Make1", "Model1", 2020, 50)); vehicles_.push_back(new Vehicle("Make2", "Model2", 2020, 60)); vehicles_.push_back(new Vehicle("Make3", "Model3", 2020, 70)); // 添加客户 customers_.push_back(new Customer("Customer1", 25)); customers_.push_back(new Customer("Customer2", 30)); } // 打印所有车辆信息 void print_all_vehicles() const { cout << "All vehicles:" << endl; for (const auto& vehicle : vehicles_) { vehicle->print_info(); } } // 打印所有客户信息 void print_all_customers() const { cout << "All customers:" << endl; for (const auto& customer : customers_) { customer->print_info(); } } private: vector<Vehicle*> vehicles_; // 所有车辆的数组 vector<Customer*> customers_; // 所有客户的数组 }; int main() { // 创建租车公司 CarRentalCompany company; // 打印所有车辆和客户信息 company.print_all_vehicles(); company.print_all_customers(); // 客户1租用第2辆车 company.print_all_customers(); company.print_all_vehicles(); company.print_all_customers(); company.print_all_vehicles(); return 0; } ``` 该代码定义了三个类:Vehicle、Customer 和 CarRentalCompany。其中,Vehicle 和 Customer 分别表示车辆和客户,而 CarRentalCompany 则表示租车公司。使用多重继承,CarRentalCompany 类同时继承了 Vehicle 和 Customer 类的属性和方法。 使用该代码,我们可以创建一个租车公司,并初始化其中的车辆和客户。然后,我们可以打印所有车辆和客户的信息,以及让某个客户租用某辆车。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值