java構(gòu)造函數(shù)示例(構(gòu)造方法)
TestCar.java
public class TestCar {
public static void main(String[] args) {
Car c1 = new Car();
c1.color = "red";
c1.brand = "xxx";//如果這輛汽車有很多屬性,這樣一一賦值不是很麻煩?有沒有辦法一生產(chǎn)出來就設(shè)定它的屬性(初始化)嗎?有~~~看下面
}
}
class Car {
String color;
String brand;
void run() {
System.out.printf("I am running...running..running~~~~\n");
}
void showMessage() {
System.out.printf("汽車顏色:%s, 汽車品牌:%s\n", color, brand);
}
}
改進后的TestCar_EX.java
/*什么是構(gòu)造方法*/
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; //這里的this是這個對象的意思.第一個color是這個對象的color屬性,第二個是局部變量color
this.brand = brand; //同上
}
void run() {
System.out.printf("I am running...running..running~~~~\n");
}
void showMessage() {
System.out.printf("汽車顏色:%s, 汽車品牌:%s\n", color, brand);
}
}
相關(guān)文章
IDEA報錯:Process terminated的問題及解決
這篇文章主要介紹了IDEA報錯:Process terminated的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11使用IDEA如何打包發(fā)布SpringBoot并部署到云服務(wù)器
這篇文章主要介紹了使用IDEA如何打包發(fā)布SpringBoot并部署到云服務(wù)器問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12springboot打war包部署到外置tomcat容器的方法
這篇文章主要介紹了springboot]打war包部署到外置tomcat容器,在這需要注意的是在boot-launch.war在tomcat?webapps目錄里面解壓到boot-launch文件夾,感興趣的朋友跟隨小編一起看看吧2022-04-04