如何例項化 java 內部類
package practive_java;
class cow {
private double weight;
public cow() {
weight=0;
}
public cow(double data) {
weight=data;
}
protected class cowleg {
private String color;
private double length;
public cowleg(double length, String color) {
this.length=length;
this.color=color;
}
public void info() {
System.out.println("current cow color " + color);
System.out.println("current cow leg length " + length);
System.out.println("current cow weight " + weight);
}
}
public void display(){
cowleg cl=new cowleg(1.12,"black");
cl.info();
}
}
public class practice {
//初始化快
{
a=6;
}
int a=9;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("practice.a=" + (new practice()).a); //OUTPUT 9
cow mycow = new cow(23.5);
mycow.display();
//例項化內部類cowleg , this inner class must be declared as protected or public
cow mycow2 = new cow(23.4);
cow.cowleg myleg= mycow2.new cowleg(12.4,"red");
myleg.info();
}
}