类的构造方法

复制代码
 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
复制代码

 

 

属性静态;

行为动态;

 

posted @   百事没事  阅读(163)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示