尝试编写一个类,定义一个修饰权限为private的成员变量,定义两个成员方法,一个方法实现为此成员变量赋值,另一个成员方法获取这个成员变量的值,保证其它类继承该类时能获取该类的成员变量的值。
编写的一个手机类:
1 package org.hanqi.pn0120; 2 3 public class Phone { 4 5 private String pinpai; 6 7 private double neicun; 8 9 public String getPinpai() { 10 return pinpai; 11 } 12 13 public void setPinpai(String pinpai) { 14 this.pinpai = pinpai; 15 } 16 17 public double getNeicun() { 18 return neicun; 19 } 20 21 public void setNeicun(double neicun) { 22 this.neicun = neicun; 23 } 24 25 private double yiyongnc; 26 27 public double yiyongnc() 28 { 29 return this.yiyongnc; 30 } 31 32 private double shengyunc; 33 34 // public double shnegyunc() 35 // { 36 // return this.shengyunc; 37 // } 38 39 public void yunxingsudu(double shengyunc ) 40 { 41 if( shengyunc>0) 42 { 43 System.out.println("手机正常运行,运行速度较快!"); 44 } 45 else if(shengyunc==0) 46 { 47 System.out.println("手机内存不足,运行速度减慢,请及时清理!"); 48 } 49 else 50 { 51 System.out.println("表闹了,你手机超神了!"); 52 } 53 54 } 55 56 public void xiazai(double yiyongnc) 57 { 58 double shengyunc=neicun-yiyongnc; 59 60 if(yiyongnc>0&&yiyongnc<16) 61 { 62 System.out.println("手机剩余内存"+shengyunc+"G"); 63 } 64 else if(yiyongnc==16) 65 { 66 System.out.println("手机内存不足,剩余"+shengyunc+"G内存"); 67 } 68 else 69 { 70 System.out.println("手机穿越了!"); 71 } 72 73 74 } 75 76 public Phone(String pinpai, double neicun) { 77 super(); 78 this.pinpai = pinpai; 79 this.neicun = neicun; 80 } 81 82 public static void main(String[] args) 83 { 84 Phone myPhone=new Phone("iphone5",16); 85 86 System.out.println("手机是"+myPhone.getPinpai()); 87 88 System.out.println("手机内存是"+myPhone.getNeicun()+"G"); 89 90 myPhone.yunxingsudu(4); 91 92 myPhone.yunxingsudu(0); 93 94 myPhone.yunxingsudu(20); 95 96 myPhone.xiazai(12); 97 98 myPhone.xiazai(16); 99 100 myPhone.xiazai(25); 101 } 102 103 }