11月25日,27日Java第五課

構造方法做用:一、初始化:成員變量
二、類工廠:對象,建立初始化;
三、person=new person();
建立對象時調用構造new
程序爲自動建立默認構造方法。
this指代當前對象能夠調用當前類的成員
調用構造方法,this出如今構造方法中的第一行。
屬性通常不在類之外的方法直接調用,須要對外提供方法:
public void setName(string name){
this.name=neme;
}
public void setAge(int age){
this.age=age;
}
獲取值時:
public string getName(){
return this.name;
}
public string getAge(){
return this.age;
}
Java引用庫跟文件目錄結構類似
包:類庫分級能夠解決類的命名衝突,類文件管理等問題。
package用來定義包
package語句必須做爲Java源文件的第一條非註釋性語句
import 導入包
import 包名.*; //導入指定句中的全部類。
封裝是面向對象的特性之一
封裝實際上把該隱藏的隱藏,該暴露的暴露,這些都須要經過Java訪問控制符來實現。
訪問控制機制
private:只能被當前類中其餘成員訪問,類外看不到
缺省:能夠被同一包中全部類訪問
protected(子類訪問權限):被聲明爲protected的成員既能夠被同一個包中的其餘類訪問。
public:任意位置能夠訪問ide

public class Student{
 //成員
 private String name;
 private String id;
 public int count=0;
 }
 public Student(string name,string id){
 this.name=name;
 this.id=id;
 }
 publicc Student(){
 }
 public void setName(string name){
 this.name=name;}
 public string getName(){
 return this.name;}
  public void setId(string id){
 this.id=id;}
 public string getId(){
 return this.id;}
 public string toString(){
 return「name:」+name+「,id:」+id;
 }

 主函數
 package
 public class Main{
 public static void main(string args[]){
 student student=new student("AA","1502001");
 student student1=new student("BB","1502002");
 system.out.println(student.tostring());
 }
 }
 靜態成員static用於輸出計算,將動態變爲靜態,

 普通方法和靜態方法是否能夠相互調用
 靜態方法不能指向非靜態方法
 靜態方法能夠在任意方法調用
相關文章
相關標籤/搜索