Testcar.java
Copy Code code as follows:
public class Testcar {
public static void Main (string[] args) {
Car C1 = new car ();
C1.color = "Red";
C1.brand = "xxx";//If the car has many properties, so one by one assignment is not very troublesome? Is there a way to set its properties (initialization) as soon as it is produced? There are ~ ~ ~ Look below
}
}
Class Car {
String color;
String brand;
void Run () {
System.out.printf ("I am running...running.") Running~~~~\n ");
}
void ShowMessage () {
System.out.printf ("Car color:%s, car brand:%s\n", color, brand);
}
}
The improved Testcar_ex.java
Copy Code code as follows:
/* What is the construction method * *
public class Testcar_ex {
public static void Main (string[] args) {
Car C1 = new Car ("Red", "xxx");
}
}
Class Car {
String color;
String brand;
Public car (string color, string brand) {
This.color = color; Here is what this object means. The first color is the color property of this object, and the second is the local variable color
This.brand = brand; Ditto
}
void Run () {
System.out.printf ("I am running...running.") Running~~~~\n ");
}
void ShowMessage () {
System.out.printf ("Car color:%s, car brand:%s\n", color, brand);
}
}