java 內部類的靜態化

剛剛寫一段代碼

報這樣的錯誤:

No enclosing instance of type Bag is accessible. Must qualify the allocation with an enclosing instance of type Bag (e.g. x.new A() where x is an instance of Bag).

代碼預覽:

package test20150402;

public class Bag {
	private String name;
	private int age;
	
	public String getName(){
		return name;
	}
	
	public void setName(String name){
		this.name = name;
	}
	
	public int getAge(){
		return age;
	}
	
	public void setAge(int age){
		this.age = age;
	}
	
	
	static class Maomao extends Bag{
		private String school;
		public String getSchool(){
			return school;
		}
		
		public void setSchool(String school){
			this.school = school;
		}
	}
	
	public static void main(String[] args){
		Maomao mm = new Maomao();
		mm.setName("pipi");
		mm.setAge(2);
		mm.setSchool("shuanglou");
		System.out.println(mm.getName());
		System.out.println(mm.getAge());
		System.out.println(mm.getSchool());
	}
}

這句報錯:

Maomao mm = new Maomao();


把Maomao這個類改成static的,問題就解決了。

發佈了73 篇原創文章 · 獲贊 11 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章