内部类

时间:2022-05-05
本文章向大家介绍内部类,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
 1 package st;
 2 //内部类
 3 class RedCowForm
 4 {
 5     static String formName;
 6     RedCow cow;
 7     RedCowForm(){}
 8     RedCowForm(String s){
 9      cow = new RedCow(150,112,5000);
10      formName=s;    
11     }
12     public void ShowCowMess()
13     {
14         cow.speak();
15     }
16     class RedCow  //内部类的声明
17     {
18         String cowName ="红牛";
19         int height , weight , price;
20         RedCow(int h ,int w, int p)
21         {
22            height=h;
23            weight=w;
24            price=p;
25         }
26    void  speak()
27    {
28      System.out.println("偶是"+cowName+",身高:"+height+"cm 体重: "+weight+"kg,生活在"+formName);
29    }
30     }
31 }
32 public class example_1
33 {
34     public static void main(String args[])
35     {
36         RedCowForm form = new  RedCowForm("红牛农场");
37         form.ShowCowMess();
38         form.cow.speak();
39     }
40 }