day04_oop

本文介绍了Java中类和对象的概念,类作为创建对象的模板,对象是类的实例。讨论了成员变量和局部变量的差异,以及参数传递的基本规则。此外,还涵盖了方法重载、构造方法的使用,以及给对象属性赋值的不同方式。同时,提到了`this`关键字的作用和静态成员的特性。
摘要由CSDN通过智能技术生成

1 类和对象的关系

对象是根据类创建的实例
类是创建对象的模板/图纸
类是对现实中一类事物的描述
对象是现实事物在电脑中的模型

让汽车跑起来

  • 创建一个类描述汽车类型
class Car{
    //定义变量表示数据
    double speed;
    char color;
    String  number;
    //定义方法表示功能
    void run(){
        sout("一辆"+color+"色的汽车,车牌是"+number+"的,正在以"+speed+"km/h的速度运行!");
    }
}
  • 根据类创建对象
Car c11=new Car();//根据类创建对象
Car c12=new Car();//根据类创建对象
Car c13=new Car();//根据类创建对象

//类中定义了那变量 对象就拥有那些数据
c11.number="豫A1111";c12.color='黑';
//类中定义了那些方法  对象就拥有那些功能
c11.run();

2 成员

成员:类的组成
类的成员: 用于描述数据的变量---成员变量
         用于描述功能的方法---成员方法
类中定义那些成员变量  此类创建的对象就拥有那些变量(对象中的变量--属性)         

变量:成员变量+局部变量

变量的作用域:取决于定义此保留的大括号
成员变量:类中定义的变量   ---作用域是整个类
局部变量:方法中定义的变量  ---当前方法
public class Demo01Variable {
	/*成员变量和局部变量区别:
	 * 1 是否有默认:成员变量有默认初始值,但局部变量没有默认初始值
	 * 2 定义位置不同:成员变量定义在类中 局部变量定义在方法中
	 * 3 作用域不同:成员变量作用域是本类  局部本类作用域是当前方法
	 * */
     public static void main(String[] args) {
		  // 调用一个类的方法:通过此类的对象类调用
    	 Test01 t1=new Test01();
    	 t1.t1(11);
	}
}
class Test01{
	int a;//成员变量
	void t1(int b) {//局部变量
		int c=1;//局部变量
		System.out.println(a+":"+b+":"+c);
	}
	void t2() {
		//System.out.println(a+":"+b+":"+c);
	}
}

3 参数传递

	public static void main(String[] args) {
		  int a=1;
		  int b=a;//b=1 a=1   吧变量a的值复制一份给了b   
		  a++;//a=2 b=1
		  System.out.println("b="+b);
		  
		  int[] arr1= {1,2,4};
		  int[] arr2=arr1;//arr2和arr1指向10行的数组对象
		  arr1[0]++;
		  System.out.println(arr2[0]);
		  
		  //注意:基本数据类型变量之间传递的是常量值:
		  //          int a=b;吧b的值复制一份给了a
		  //      引用数据类型变量之间传递的对象的内存地址
		  //          int[] arr1=arr2; arr1和arr2指向同一个对象
		  
		  int c=11;
		  testInt(c);//等价于:int a=c;a++
		  System.out.println("c="+c);
		  
		  int[] arr= {1,2,3};
		  testArr(arr);//等价于:int[] array=arr;array[0]++;
		  printArr(arr);
		  
		  
	}
	//基本数据类型作为参数传递的是常量值:
	static void testInt(int a) {
		a++;
	}
	//引用数据类型作为参数传递的对象的内存地址
	static void testArr(int[] array) {
		array[0]++;
	}
	static void printArr(int[] arr) {
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i]+(i==arr.length-1?"\r\n":","));
		}
	}

在这里插入图片描述

4 方法重载:overload

概念

同一个类中 几个方法 方法名相同 参数列表不同的现象
public class Demo03Overload {
	public static void main(String[] args) {
		//实参:方法在调用时传递的数据
		//形参:方法在定义时 定义的参数列表
		//同一个类中 几个方法 方法名相同 参数列表不同的现象
		//重载的方法:调用时 不是通过方法名来区分 而是通过参数类型来区分
		//重载的方法:功能一致 只是需要的原始数据不同而已
		
		//重载要求:1 同一个类  2方法名相同  3 参数列表不同(参数个数不同,参数类型不同,参数顺序不同)
		//         其他方法声明无关
		
		//方法由两部分组成:方法声明部分+方法体部分
		
		Student3 s1=new Student3();
		s1.add1(1, 2);
		s1.add1(1, 2.1);
	}
}
class Student3{
	String name;
	void show() {
		System.out.println("show:::"+name);
	}
	//求和
	int add1(int a,int b) {
		System.out.println("int add1(int a,int b)");
		return a+b;
	}
	double add1(int a,double b) {
		System.out.println("double add1(int a,double b) ");
		return a+b;
	}
	double add1(double a,int b) {
		System.out.println("double add1(int a,double b) ");
		return a+b;
	}
	int add1(int a,int b,int c) {return a+b+c;}
	//重载的要求:
	//static int add1(int a,int b,int c) {return 1;}
}

5 构造方法:constructor:构造器

概念

public class Demo04Constructor {
	/*
	 * 构造方法:contructor:
	 * 类中定义的 用于构建创造对象的方法--构造方法
	 * 构造方法特点:
	 * 1 构造方法名字必须和类名一致
	 * 2 构造方法没有返回值 不用void标识
	 * 3 构造方法被new 调用,调用一次就创建一个对象
	 * 4 如果一个类没有构造方法   编译器默认添加一个无参数的构造方法
	 * 5 构造方法不能被对象调用
	 * 6 构造方法的参数列表 一般用于给成员变量赋值
	 * 
	 * */
	public static void main(String[] args) {
		  //构建类创建对象:对象类型 引用名=new 对象类型();
		    Student4 s1=new Student4();
		    s1=new Student4();
		    //分析:创建对象:new Student4();
		    //new是创建对象的关键字
		    //Student4():()是方法的关键字 说明这是个方法
		    //Student4()方法在Student4中没有看到 但是可以使用 说明默认有方法Student4()
		    
		    //s1.show();
	}
}
class Student4{
	int age;
	void show() {
		System.out.println("show::"+age);
		//return;
	}
	Student4() {
		System.out.println("Student4() ");
	}
	Student4(int a) {
		age=a;
		System.out.println("Student4(int a) ");
	}
	Student4(int a,int b) {
		System.out.println("Student4(int a,int b) ");
	}
	void Student4() {//warn:This method has a constructor name
		System.out.println("void Student4() ");
	}
}

构造方法和普通方法的区别

1:格式不同:普通方法必须有返回值类型标识 如果没有返回值用void标识
           构造方法没有返回值 不用void标识
2:命名不同:构造方法名字必须是类名
           普通方法名字可以是类名(但如果是类名 不规范)
3:调用不同:构造方法被关键字new调用 调用一次就创建一个新的对象
           普通方法被对象调用 一个对象可以调用多次普通方法
4:意义不同:构造方法功能用于创建对象
           普通方法表示当前类创建的对象具有的功能

6 给对象的属性赋值方式

public class Demo05Set {
	public static void main(String[] args) {
		Car5 c11=new Car5();
		Car5 c12=new Car5();
		Car5 c13=new Car5();
		
		//给对象的属性赋值:
		c11.color='红';//2:对象.属性 给属性赋值:特点:必须先创建对象 只能更改当前对象的属性值:::类似于自己喷漆
		//c11.money=-1; //此方式被禁止:类无法控制值的范围
		
		Car5 c14=new Car5("豫A88888");//3:通过构造方法参数列表给属性赋值:特点:对象一创建属性就有指定值 只能更改当前对象的属性值:::出场定制
		c11.show();
		c12.show();
		
		c13.setSpeed(-100);//4:通过普通方法参数列表给属性赋值:特点:必须先创建对象  只能更改当前对象的属性值:::类似于4s店改装
		c13.show();
		c14.show();
	}

}
class Car5{
	String number;
	char color;
	float money=100000;//1:在类中直接给成员变量赋值  特点:此类创建的对象的此属性默认都是此值  类似于修改图纸
	int speed;
	void show() {
		System.out.println(color+":"+number+":"+money+":"+speed);
	}
	Car5(){}
	Car5(String n){number=n+"0000";}
	
	void setSpeed(int s) {
		if(s<0) {return;}
		speed=s+1;
	}
}

7 this

public class Demo06This {
	/*this:关键字  表示的当前对象:::类似于现实中的我自己
	 *使用场景1: 成员变量与局部变量重名时 方法中的变量名指向的局部变量
	 *          通过this.变量名来指向成员变量
	 *          注意:方法中调用成员 前面默认有this.
	 *使用场景2:构造方法之间的相互调用 通过this(参数列表)
	 *           注意: this(参数列表)必须是第一个语句
	 *                 
	 * */
	public static void main(String[] args) {
//		Demo06 d1=new Demo06();
//		d1.show();
//		System.out.println(d1.a);
//		
//		Demo06 d2=new Demo06();
//		d2.a=11;
//		d2.show();
		new Demo06(1);
		
	}
}
class Demo06{
	int a=1;//成员变量
	int b=11;
	void show() {
		int a=2;//局部变量
		System.out.println("a="+a);//a=2  就近原则:局部变量a
		System.out.println("this.a="+this.a);//a=1  当前对象的a
		System.out.println("b="+b);
		System.out.println("this.b="+this.b);
		hehe();
		this.hehe();
	}
	void hehe() {
		System.out.println("-----");
		
	}
	Demo06(){
		System.out.println("我是构造方法——————");
	}
	Demo06(int a){
		this();//Constructor call must be the first statement in a constructor
		this.a=a;
		//System.out.println("我是构造方法——————");
	}
	Demo06(int a,int b,int c,int d,int e){
		
	}
	Demo06(int a,int b,int c,int d,int e,int f){
		this(a,b,c,d,e);
		//this.f=f;
	}
}

8 创建对象内存图

学习方法:

语法学习:记忆  特点:内容多 基础 乱
oop学习:1 理解概念
        2  5 张内存图

创建对象内存图

在这里插入图片描述

8 static

关键字:修饰符:静态的
public class Demo02Static {
     /*static: 修饰符 静态的
      *static修饰变量:共享数据
      *1  static修饰的变量 不但可以被对象调用  还可以被类名直接调用
      *2  static修饰的变量是共享数据 只有一份 一旦更改 所有对象的此属性都被更改
      *static修饰方法:
      *1  static修饰的方法 不但可以被对象调用  还可以被类名直接调用
      *2  static修饰的方法 只能调用static成员 不能调用非static成员
      * 
      * 
      *类成员:static修饰的成员(成员变量+成员方法)   ---属于类的
      *实例成员:非static修饰的成员                                    ---属于对象的
      *
      *类变量:static修饰的变量----共享数据
      *实例变量:非static修饰的变量----只能被当前对象调用
      *类方法:static修饰的方法
      *实例方法:非static修饰的方法
      *
      *类加载时:会为本类在内存中定义一个区域 作为静态区域:加载本类的静态成员
      *
      *什么情况下把一个变量定义为静态变量:数据是共享数据 只有一份 需要一变都变时
      *什么情况下把一个方法定义为静态方法:当一个方法不涉及实例成员时 建议定义为静态方法
      *
      *
      *
      *
      * */
	public static void main(String[] args) {
//		// TODO Auto-generated method stub
//		Student02  s11=new Student02();
//		s11.name="韩梅梅1";s11.banJi="java43";s11.ysjLogo="小天鹅";s11.guoJi="中国";
//		Student02  s12=new Student02();
//		s12.name="韩梅梅2";s12.banJi="java43";s12.ysjLogo="小天鹅";s12.guoJi="中国";
//		Student02  s13=new Student02();
//		s13.name="韩梅梅3";s13.banJi="java43";s13.ysjLogo="小天鹅";s13.guoJi="中国";
//		Student02  s14=new Student02();
//		s14.name="韩梅梅4";s14.banJi="java43";s14.ysjLogo="小天鹅";s14.guoJi="中国";
//		//每个对象都拥有一套类中定义的只属于的自己的成员::每个对象都有自己的name,age,banJI和show
//		//The static field Student02.guoJi should be accessed in a static way
//		Student02.guoJi="中华人民共和国";
//		s11.guoJi="中华民国";
//		s11.show();
//		s12.show();
//		s13.show();
//		s14.show();
//		s11.hehe();
//		s11.haha();
		Student02.haha();
		

	}
}
class Student02{
	static void haha() {
		System.out.println("static void haha()");
		//hehe();//Cannot make a static reference to the non-static method hehe() from the type Student02
		//System.out.println("学生:"+name+","+guoJi+","+age+","+banJi+","+ysjLogo);
		// Cannot make a static reference to the non-static field  banJi
	}
	String name;
	int age;
	static String guoJi;
	String banJi;
	String ysjLogo;
	Student02(){}
	void show() {
		System.out.println("学生:"+name+","+guoJi+","+age+","+banJi+","+ysjLogo);
	}
	void hehe() {
		System.out.println("void hehe()");
		haha();
		System.out.println("学生:"+name+","+guoJi+","+age+","+banJi+","+ysjLogo);
	}
}

静态内存图

在这里插入图片描述

9 final

  • 0
    点赞
  • 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、付费专栏及课程。

余额充值