// 創(chuàng)建類
class ClassName{
field; // 字段(屬性) 或者 成員變量
method; // 行為 或者 成員方法
}
class PetDog {
public String name;//名字
public String color;//顏色
// 狗的屬性
public void barks() {
System.out.println(name + ": 旺旺旺~~~");
}
// 狗的行為
public void wag() {
System.out.println(name + ": 搖尾巴~~~");
}
}
- 一般一個(gè)文件當(dāng)中只定義一個(gè)類
- main方法所在的類一般要使用public修飾(注意:Eclipse默認(rèn)會(huì)在public修飾的類中找main方法)
- public修飾的類必須要和文件名相同
- 不要輕易去修改public修飾的類的名稱,如果要修改,通過(guò)開發(fā)工具修改
public class Main{
public static void main(String[] args) {
PetDog dogh = new PetDog(); //通過(guò)new實(shí)例化對(duì)象
dogh.name = "阿黃";
dogh.color = "黑黃";
dogh.barks();
dogh.wag();
PetDog dogs = new PetDog();
dogs.name = "阿黃";
dogs.color = "黑黃";
dogs.barks();
dogs.wag();
}
}
輸出結(jié)果:
阿黃: 旺旺旺~~~
阿黃: 搖尾巴~~~
賽虎: 旺旺旺~~~
賽虎: 搖尾巴~~~
- new 關(guān)鍵字用于創(chuàng)建一個(gè)對(duì)象的實(shí)例.
- 使用 . 來(lái)訪問(wèn)對(duì)象中的屬性和方法.
- 同一個(gè)類可以創(chuàng)建對(duì)個(gè)實(shí)例.
this引用
- this的類型:對(duì)應(yīng)類類型引用,即哪個(gè)對(duì)象調(diào)用就是哪個(gè)對(duì)象的引用類型
- this只能在"成員方法"中使用
- 在"成員方法"中,this只能引用當(dāng)前對(duì)象,不能再引用其他對(duì)象
- this是“成員方法”第一個(gè)隱藏的參數(shù),編譯器會(huì)自動(dòng)傳遞,在成員方法執(zhí)行時(shí),編譯器會(huì)負(fù)責(zé)將調(diào)用成員方法 對(duì)象的引用傳遞給該成員方法,this負(fù)責(zé)來(lái)接收

public class Date {
public int year;
public int month;
public int day;
// 構(gòu)造方法:
// 名字與類名相同,沒有返回值類型,設(shè)置為void也不行
// 一般情況下使用public修飾
// 在創(chuàng)建對(duì)象時(shí)由編譯器自動(dòng)調(diào)用,并且在對(duì)象的生命周期內(nèi)只調(diào)用一次
public Date(int year, int month, int day){
this.year = year;
this.month = month;
this.day = day;
System.out.println("Date(int,int,int)方法被調(diào)用了");
}
public void printDate(){
System.out.println(year + "-" + month + "-" + day);
}
public static void main(String[] args) {
// 此處創(chuàng)建了一個(gè)Date類型的對(duì)象,并沒有顯式調(diào)用構(gòu)造方法
Date d = new Date(2021,6,9); // 輸出Date(int,int,int)方法被調(diào)用了
d.printDate(); // 2021-6-9
}
}
- 名字必須與類名相同
- 沒有返回值類型,設(shè)置為void也不行
- 創(chuàng)建對(duì)象時(shí)由編譯器自動(dòng)調(diào)用,并且在對(duì)象的生命周期內(nèi)只調(diào)用一次
- 構(gòu)造方法可以重載(用戶根據(jù)自己的需求提供不同參數(shù)的構(gòu)造方法)
public class Date {
public int year;
public int month;
public int day;
// 無(wú)參構(gòu)造方法--內(nèi)部給各個(gè)成員賦值初始值,該部分功能與三個(gè)參數(shù)的構(gòu)造方法重復(fù)
// 此處可以在無(wú)參構(gòu)造方法中通過(guò)this調(diào)用帶有三個(gè)參數(shù)的構(gòu)造方法
// 但是this(1900,1,1);必須是構(gòu)造方法中第一條語(yǔ)句
public Date(){
//System.out.println(year); 注釋取消掉,編譯會(huì)失敗
this(1900, 1, 1);
//this.year = 1900;
//this.month = 1;
//this.day = 1;
}
// 帶有三個(gè)參數(shù)的構(gòu)造方法
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
}
- this(...)必須是構(gòu)造方法中第一條語(yǔ)句
- 不能形成環(huán)
public class Date {
public int year = 1900;
public int month = 1;
public int day = 1;
public Date(){
}
public Date(int year, int month, int day) {
}
public static void main(String[] args) {
Date d1 = new Date(2021,6,9);
Date d2 = new Date();
}
}
封裝
訪問(wèn)限定符文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-729380.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-729380.html
public class Computer {
private String cpu; // cpu
private String memory; // 內(nèi)存
public String screen; // 屏幕
String brand; // 品牌---->default屬性
public Computer(String brand, String cpu, String memory, String screen) {
this.brand = brand;
this.cpu = cpu;
this.memory = memory;
this.screen = screen;
}
public void Boot(){
System.out.println("開機(jī)~~~");
}
public void PowerOff(){
System.out.println("關(guān)機(jī)~~~");
}
public void SurfInternet(){
System.out.println("上網(wǎng)~~~");
}
}
public class TestComputer {
public static void main(String[] args) {
Computer p = new Computer("HW", "i7", "8G", "13*14");
System.out.println(p.brand); // default屬性:只能被本包中類訪問(wèn)
System.out.println(p.screen); // public屬性: 可以任何其他類訪問(wèn)
// System.out.println(p.cpu); // private屬性:只能在Computer類中訪問(wèn),不能被其他類訪問(wèn)
}
}
到了這里,關(guān)于JavaSE | 初識(shí)Java(八) | 類和對(duì)象的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!