每日代码系列(23)

 1 class RedCowForm {
 2   static String formName;
 3   RedCow cow;  //内部类声明对象
 4     RedCowForm() {}  //不写对输出结果没有影响
 5   RedCowForm(String s) {
 6     cow=new RedCow(150,112,5000);
 7     formName=s;
 8   }
 9   public void showCowMess() {
10     cow.speak();
11   }
12   class RedCow {  //内部类的声明
13     String cowName="红牛";
14     int height,weight,price;
15     RedCow(int h,int w,int p) {
16       height=h;
17       weight=w;
18       price=p;
19     }
20     void speak() {
21       System.out.println("偶是"+cowName+",身高:"+height+"cm 体重"+weight+"kg,生活在"+formName);
22     }
23   }  //内部类结束
24 }    //外部类结束
25 public class Example7_1 {
26   public static void main(String[] args) {
27     RedCowForm form=new RedCowForm("红牛农场");
28     form.showCowMess();
29     form.cow.speak();
30   }
31 }
原文地址:https://www.cnblogs.com/ljydbk/p/14154423.html