![]()
1 //所有数值未做合法性检测
2
3 //西游记人物类
4 class XiYouJiRenWu{
5 //成员属性
6 private String name; //人物姓名
7 private double height; //身高
8 private String weapon; //武器
9 //构造方法
10 XiYouJiRenWu(){
11 }
12 XiYouJiRenWu(String name){
13 this();
14 this.name=name;
15 }
16 XiYouJiRenWu(String name,double height){
17 this(name);
18 this.height=height;
19 }
20 XiYouJiRenWu(String name,double height,String weapon){
21 this(name,height);
22 this.weapon=weapon;
23 }
24 //setter
25 public void setName(String name){
26 this.name=name;
27 }
28 public void setHeight(double height){
29 this.height=height;
30 }
31 public void setWeapon(String weapon){
32 this.weapon=weapon;
33 }
34 //getter
35 public String getName(){
36 return name;
37 }
38 public double getHeight(){
39 return height;
40 }
41 public String getWeapon(){
42 return weapon;
43 }
44 }
45 //text class
46 public class Text4{
47 public static void main(String[] args){
48 XiYouJiRenWu sun=new XiYouJiRenWu("孙悟空",175);
49 sun.setWeapon("金箍棒");
50 System.out.println("姓名:" + sun.getName() + " 身高:" + sun.getHeight() + " 武器是:" + sun.getWeapon());
51 XiYouJiRenWu zhu=new XiYouJiRenWu("猪八戒",150);
52 zhu.setWeapon("九尺杷");
53 System.out.println("姓名:" + zhu.getName() + " 身高:" + zhu.getHeight() + " 武器是:" + zhu.getWeapon());
54 }
55 }
1 //所有数值未做合法性检测
2
3 //Account class
4 class Account{
5 //成员属性
6 private String account; //定义账号
7 private double balance; //定义存款余额
8 //构造方法
9 Account(){
10 }
11 Account(String account){
12 this();
13 this.account=account;
14 }
15 Account(String account,double balance){
16 this(account);
17 this.balance=balance;
18 }
19 //getter
20 public String getAccount(){
21 return account;
22 }
23 public double getBalance(){
24 return balance;
25 }
26 //成员方法
27 //存款方法
28 public void deposit(double money){
29 balance=this.getBalance()+money;
30 System.out.println("账户:" + this.getAccount() + "中成功存入:" + money + "元!");
31 }
32 //取款方法
33 public void quKuan(double money){
34 balance=this.getBalance()-money;
35 System.out.println("账户:" + this.getAccount() + "中成功取款:" + money + "元!");
36 }
37 //查询余额
38 public void search(){
39 System.out.println("账户:" + this.getAccount() + "中余额为:" + this.getBalance());
40 }
41 }
42 //Text5 class
43 public class Text5{
44 public static void main(String[] args){
45 Account account=new Account("6333081603000420028",1000000);
46 account.search();
47 account.deposit(1000);
48 account.quKuan(50000);
49 account.search();
50 }
51 }
1 //所有数值未做合法性检测
2
3 class Clock{
4 private int hour;
5 private int minute;
6 private int seconds;
7 Clock(){
8 }
9 Clock(int hour){
10 this();
11 this.hour=hour;
12 }
13 Clock(int hour,int minute){
14 this(hour);
15 this.minute=minute;
16 }
17 Clock(int hour,int minute,int seconds){
18 this(hour,minute);
19 this.seconds=seconds;
20 }
21 public void setHour(int hour){
22 this.hour=hour;
23 }
24 public void setMinute(int minute){
25 this.minute=minute;
26 }
27 public void setSeconds(int seconds){
28 this.seconds=seconds;
29 }
30 public int getHour(){
31 return hour;
32 }
33 public int getMinute(){
34 return minute;
35 }
36 public int getSeconds(){
37 return seconds;
38 }
39 public void show(){
40 System.out.println("当前时间为:" + this.getHour() + "点" + this.getMinute() + "分" + this.getSeconds() + "秒!");
41 }
42 }
43 public class Text6{
44 public static void main(String[] args){
45 Clock c1=new Clock();
46 c1.show();
47 c1.setHour(10);
48 c1.show();
49 Clock c2=new Clock(10,10,10);
50 c2.show();
51 }
52 }
53