标签:
1 package com.hanqi; 2 3 //父类 4 5 //电话 6 public class telephone { 7 8 public telephone() 9 { 10 11 } 12 13 14 15 public telephone (String pinpai,String haoma) 16 { 17 this.pinpai =pinpai; 18 this.haoma =haoma; 19 System.out.println("品牌是"+this.pinpai+";号码是"+this.haoma); 20 } 21 22 //品牌 23 private String pinpai; 24 25 //setter方法 26 27 /*public void setPinpai(String pinpai) 28 { 29 this.pinpai = pinpai; 30 }*/ 31 32 //getter方法 33 public String getPinpai() 34 { 35 return pinpai; 36 } 37 //公共 —— public 开放的 38 //私有 —— private 本类内 39 //保护 —— protected 本包内的类 (默认的) 40 //号码 41 private String haoma; //私有的 42 43 //getter 方法 44 public String getHaoma() 45 { 46 return haoma; 47 } 48 //电量 49 50 private int dianliang=50; 51 //设置电量 52 public void setDianliang(int dianliang) 53 { 54 //验证 55 if (dianliang<0){ 56 57 58 System.out.println("电量不能为负值"); 59 } 60 else if(dianliang>100) 61 { 62 System.out.println("电量满了"); 63 this.dianliang =dianliang; 64 } 65 else 66 { 67 this.dianliang=dianliang; 68 } 69 this.dianliang=dianliang; 70 } 71 72 //获取电量 73 public int getDanliang() 74 { 75 return dianliang; 76 } 77 public void call(String duifanghaoma) 78 { 79 if (duifanghaoma.trim().length() > 0) 80 { 81 System.out.println("我播出了电话 "+duifanghaoma); 82 } 83 else 84 { 85 System.out.println("您输入的电话号码有误"); 86 } 87 } 88 89 public static String morenhaoma; 90 91 public static void yijianbohao() 92 { 93 94 System.out.println("一键拨号拨打 "+ morenhaoma ); 95 96 } 97 98 99 100 public static void jinji() 101 { 102 System.out.println("紧急拨打119"); 103 } 104 105 106 107 108 109 } 110 111 112 113 114 package com.hanqi; 115 116 //子类 117 118 //继承的关键字 extends 119 120 public class iphone extends telephone { 121 122 public iphone(String pinpai, String haoma) 123 { 124 //调用父类的构造方法` 125 super(pinpai, haoma); 126 } 127 128 public iphone() 129 { 130 131 } 132 public iphone(String zhanghao) 133 { 134 135 } 136 137 public void playGame(String gameName) 138 { 139 System.out.println("我喜欢玩"+ gameName); 140 } 141 public void playMusic(String musicName) 142 { 143 System.out.println("我喜欢听"+ musicName); 144 } 145 146 //重写父类的方法(重构) 147 public void call(String duifanghaoma) 148 { 149 System.out.println("我用苹果拨打了"+duifanghaoma); 150 151 } 152 153 } 154 155 156 157 158 package com.hanqi; 159 160 //子类 161 162 //继承的关键字 extends 163 164 public class iphone extends telephone { 165 166 public iphone(String pinpai, String haoma) 167 { 168 //调用父类的构造方法` 169 super(pinpai, haoma); 170 } 171 172 public iphone() 173 { 174 175 } 176 public iphone(String zhanghao) 177 { 178 179 } 180 181 public void playGame(String gameName) 182 { 183 System.out.println("我喜欢玩"+ gameName); 184 } 185 public void playMusic(String musicName) 186 { 187 System.out.println("我喜欢听"+ musicName); 188 } 189 190 //重写父类的方法(重构) 191 public void call(String duifanghaoma) 192 { 193 System.out.println("我用苹果拨打了"+duifanghaoma); 194 195 } 196 197 }
标签:
原文地址:http://www.cnblogs.com/ymf123/p/4883777.html