类的构造方法

 1 package HQ;
 2 
 3 
 4 
 5 //
 6 public class Bird {
 7 
 8     
 9     //属性
10         //颜色,重量
11     String Color;
12     double weight;
13     
14     //行为    用方法,后边加()
15         //飞,吃
16     void fly()//void没有返回值
17     {
18         System.out.println("会飞");
19     }
20     
21     void eat()
22     {
23         System.out.println("会吃");
24     }
25     
26     public static void main(String[]args)
27     {
28         
29         //生成一只老鹰
30         
31         Bird eagle=new Bird();
32         
33         
34         eagle.Color="灰色";
35         eagle.weight=10;
36         
37         System.out.println("这是一只鸟,颜色是:"+eagle.Color);
38         
39         //调用方法
40         eagle.fly();
41         eagle.eat();
View Code

 1 package HQ;
 2 
 3 
 4 
 5 public class phone {
 6     
 7     String color;
 8     double lenth;
 9     
10     
11     void a()
12     {
13         System.out.println("会响");
14     }
15     void b()
16     {
17         System.out.println("会动");
18     }
19     
20     public static void main(String[]args)
21     {
22         
23         //生成一个手机
24         phone apple=new phone();
25         
26         apple.color="黑色";
27         apple.lenth=12;
28         
29         System.out.println("手机的颜色是:"+apple.color);
30         System.out.println("手机的长度是:"+apple.lenth);
31                 
32         
33         apple.a();
34         apple.b();
35         
36     }
View Code

属性静态;

行为动态;

原文地址:https://www.cnblogs.com/1ming/p/5235408.html