【Java】6.3 类和对象 Class and Object 04 关键词this,Object类,总结

【this操作符】
有的时候,我们希望能够继续使用相同的变量名,但是,如果我们为参数选择的名称与为属性选择的名称相同,则会出现编译器错误
但是,如果我们在Java中使用特殊的this关键字,则可以区分作为参数传递的变量和属于类实例的变量

比如我们现在想要设置Car中一些参数的数值,我们就可以利用this关键字,从而不需要再取名字

public Car(int numberOfDoors, String colour,char motorType)
{
        this.numberOfDoors = numberOfDoors;  //使某个实例的numberOfdoors设置为该方法的传入值
	this.colour = colour;
	this.motorType = motorType;
}

【对象类 Object class 】
在Java中,每个类实际上都是非常重要的类的子类,这个“非常重要的类”就是Object类
Object类具有许多方法,这些方法可用于所有其他类

比如,在对象应用程序编程接口(API)中,toString()被描述为“返回此对象的字符串表示形式”的方法
在类中重写toString()方法以打印出与该类相关的所有信息是一种很好的做法。

之前我们用来打印的方法

public void printCarDetails()
{
	String details = "Number of doors: " + numberOfDoors;
	details += "\nColour: " + colour;
	details += "\nMotor Type: " + motorType;
	details += "Car Reg: " + carReg;
	System.out.println(details);
}

修改后的代码

public String toString()
{
	String details = "Number of doors: " + numberOfDoors;
	details += "\nColour: " + colour;
	details += "\nMotor Type: " + motorType;
	details += "Car Reg: " + carReg;
		
	return details;
}  

【类和对象的总结】
1.可以将类视为模板。该模板可以是更通用的东西的蓝图
2.类的主要组件是:
•类名
•属性,也称为实例变量
•构造函数
•访问器方法(也称为Getter方法)
•更改器方法(也称为Setter方法)
•以方法作为形式的功能
3.每个类都应该有一个默认构造函数和一个通用构造函数,在创建类的对象时,我们可以使用默认构造函数和通用构造函数
4.Accessors和mutator方法用于获取和设置类中的不同属性
5.访问修饰符-设置对变量,方法,类或包的访问级别
非访问修饰符-以其他(与访问无关)的方式更改变量,方法,类或包
6.Java中的this关键词可用于区分作为参数传递的变量和属于类实例的变量
7.在所有类中重写toString()方法以打印出与该类相关的所有信息是一种很好的做法
8.相关class之间的调用,需要它们在同一目录下

【之前提到的实例代码完整版】
Car:

public class Car
{
	//Attributes 
	private int numberOfDoors;
	private String colour;
	private char motorType;  //I = ICE, E = Electric F = fuelcell
	private int carReg;
	private static int lastCarReg = 1000;
	
	//Constructors
	
	//Default Constructor
	public Car()
	{
		numberOfDoors = 4;
		colour = "Black";
		motorType = 'I';
		lastCarReg++;   //increase the value 
		carReg = lastCarReg; // assign that value to the reg of this Car instance
	}
	
	public Car(int numberOfDoors, String colour, char motorType)
	{
		this.numberOfDoors = numberOfDoors;
		this.colour = colour;
		this.motorType = motorType;
		lastCarReg++;
		carReg = lastCarReg;
	}
	
	public Car(int doors)
	{
		numberOfDoors = doors;
		colour = "Black";
		motorType = 'I';
	}
	
	//Getter methods
		//get the colour
	public String getColour()
	{
		return colour;
	}
	public int getDoors()
	{
		return numberOfDoors;
	}
	public char getMotorType()
	{
		return motorType;
	}
	public int getReg()
	{
		return carReg;
	}
	
	//Setter Methods
	public void setDoors(int num)
	{
		numberOfDoors = num;
	}
	public void setColour(String col)
	{
		if(col.equals("Red") || col.equals("Green")){
			colour = col;	
		}
	}
	
	//Functionality
	public void printCarDetails()
	{
		String details = "Number of doors: " + numberOfDoors;
		details += "\nColour: " + colour;
		details += "\nMotor Type: " + motorType;
		details += "Car Reg: " + carReg;
		System.out.println(details);
	}
	
	public String toString()
	{
		String details = "Number of doors: " + numberOfDoors;
		details += "\nColour: " + colour;
		details += "\nMotor Type: " + motorType;
		details += "Car Reg: " + carReg;
		
		return details;
	}
	
	
	/*
	public Car(int numberOfDoors, String colour,char motorType)
	{
		this.numberOfDoors = numberOfDoors;
		this.colour = colour;
		this.motorType = motorType;
	}
	
	public void setColour(String colour)
	{
		this.colour = colour;
	}
	*/
	
}

testCar:

public class TestCar{
	public static void main(String args[]){
		//Create a Car instance by calling the default constructor
		Car c1 = new Car();
		
		//Create another Car instance by calling our general constructor with 3 parameters
		Car c2 = new Car(5,"red",'E');
		Car c3 = new Car(16);
		
		//Call printCarDetails method on each car
		c1.printCarDetails();
		c2.printCarDetails();
		c3.printCarDetails();
		
		System.out.println("Colours:");
		System.out.println("The colour of Car C1 : " + c1.getColour());
		System.out.println("The colour of Car C2 : " + c2.getColour());
		System.out.println("The colour of Car C3 : " + c3.getColour());
		c1.setColour("Green");
		c3.setDoors(4);
		
		c1.printCarDetails();
		c3.printCarDetails();
		System.out.println(c1.toString());
		
	}
}
posted @ 2021-05-18 20:23  RetenQ  阅读(79)  评论(0编辑  收藏  举报