建立對象
面對對象:以類的方式組織代碼,以對象組織(封裝)資料。
抽象
三大特性:封裝性、繼承性,多态性
初始化對象使用new關鍵字建立對象
類中的構造方法:1、必須和類的名字相同。2、必須設有傳回類型,也不能寫void
public class mate{
String name;
int age;
int sno;
}
=====================================================================
public class Student {
public static void main(String [] agrd){
mate xiaoMin= new mate();//初始化
mate xiaohong=new mate();
xiaoMin.name ="xiaomin";
xiaoMin .age =19;
xiaoMin .sno =1213;
xiaohong .name ="xiaohong";
xiaohong .age =18;
xiaohong .sno =1214;
System.out.println(xiaoMin .name ) ;
}
}
class Qishi {
private int leve;
private String name;
private int hp;
private int mp; //protected,private,public ==>面向對象程式設計 都有 //函數的 重載和重寫 //不帶參數的構造函數 => public + 類名
public Qishi() {} //帶參的構造函數
public Qishi(int leve,String name,int hp,int mp) {
this.leve = leve;
this.name = name;
this.hp = hp;
this.mp = mp;
} //構造方法
public void setName(String name) {
}
public String getName() {
return this.name;
public void setLeve(int leve) {
public int getLeve() {
return this.leve;
public void setHp(int hp) {
public int getHp() {
return this.hp;
public void setMp(int mp) {
public int getMp() {
return this.mp;
這裡如果沒有寫構造方法的話系統是會預設生成構造函數的。
對象的應用:
引用類型:基本類型
對象是通過引用來操作的:棧-->堆
屬性:字段field 成員變量
預設初始化:
數字:0 0.0
char:u000
布爾值:false
引用:null
修飾符 屬性類型 屬性名=屬性值
對象的建立和使用:
必須使用new
對象屬性、對象方法