感謝大家對IT十八掌大數據的支持,今天的作業如下
利用白富美接口案例,土豪徵婚使用匿名內部類對象實現。
2.定義三角形類Trianle,裏面包含三個int類型屬性,分別表示三條邊的長度,
構造三角形對象時,任意兩邊之和是否大於第三邊,如若不成立,拋出自定義異常。
3.Person類中增加birthday屬性,對setBirthday(int ,int , int )方法進行異常處理,
要求年有效、月有效、日有效、年月日指定的具體日期有效,對不同情況分別拋出不同的異常。
4.將類定義到指定的包下。com.it18zhang,編譯之後,打成jar文件。
5.相互之間使用jar包,放置cp下,對class進行重用。
6.設計程序,考查修飾符。public -> protected -> default -> private(選做題)
7.預習多線程。
-------------------------------------------------------------------------------------------
1.利用白富美接口案例,土豪徵婚使用匿名內部類對象實現。
答:
//白
interface IWhite {
public void white();
}
// 富
interface IRich {
public void rich();
}
// 美
interface IBeauti {
public void beauti();
}
// 定義新的接口,繼承三個接口
interface IWRB extends IWhite, IRich, IBeauti {
}
class RichMan {
public void marry(IWhite i) {
System.out.println("白好");
i.white();
}
public void marry(IRich i) {
System.out.println("富很好");
i.rich();
}
public void marry(IBeauti i) {
System.out.println("美非常好");
i.beauti();
}
public void marry(IWRB i) {
System.out.println("白富美最好不過");
i.white();
i.rich();
i.beauti();
}
}
class MarriyDemo {
public static void main(String[] args) {
RichMan rm = new RichMan();
rm.marry(new IWhite() {
public void white() {
System.out.println("我很白");
}
});
rm.marry(new IRich() {
public void rich() {
System.out.println("我很富");
}
});
rm.marry(new IBeauti() {
public void beauti() {
System.out.println("我很美");
}
});
rm.marry(new IWRB() {
public void white() {
System.out.print("我白");
}
public void rich() {
System.out.print("我富");
}
public void beauti() {
System.out.println("我美");
}
});
}
}
2.定義三角形類Trianle,裏面包含三個int類型屬性,分別表示三條邊的長度,
構造三角形對象時,任意兩邊之和是否大於第三邊,如若不成立,拋出自定義異常。
答:
class EdgeTooSmallException extends Exception { // 這個異常是用來判斷邊小於或等於0的
private String info;
public EdgeTooSmallException() {
}
public EdgeTooSmallException(String info) {
this.info = info;
}
public String getInfo() {
return info;
}
public void setEdgeInfo(String info) {
this.info = info;
}
}
class EdgeNotMatchException extends Exception { // 這個異常是用來判斷三角形的
private String info;
public EdgeNotMatchException() {
}
public EdgeNotMatchException(String info) {
this.info = info;
}
public String getInfo() {
return info;
}
public void setEdgeNotMatchException(String info) {
this.info = info;
}
}
class Triangle {
//三條邊
private int edgea;
private int edgeb;
private int edgec;
// edgea的get/set方法
public int getEdgea() {
return edgea;
}
public void setEdgea(int edgea) throws EdgeTooSmallException {
if (edgea > 0) {
this.edgea = edgea;
} else {
throw new EdgeTooSmallException("邊長A不能小於或等於0");
}
}
// edgeb的get/set方法
public int getEdgeb() {
return edgeb;
}
public void setEdgeb(int edgeb) throws EdgeTooSmallException {
if (edgeb > 0) {
this.edgeb = edgeb;
} else {
throw new EdgeTooSmallException("邊長B不能小於或等於0");
}
}
// edgec的get/set方法
public int getEdgec() {
return edgec;
}
public void setEdgec(int edgec) throws EdgeTooSmallException {
if (edgec > 0) {
this.edgec = edgec;
} else {
throw new EdgeTooSmallException("邊長C不能小於或等於0");
}
}
// 判斷是不是三角形
public static void Checking(int x, int y, int z)
throws EdgeNotMatchException {
if (x + y <= z || x + z <= y || y + z <= x) {
throw new EdgeNotMatchException("這不是三角形");
} else {
System.out.println("這是個三角形");
}
}
}
class TriangleDemo {
public static void main(String[] args) {
// 構造三角形
Triangle t1 = new Triangle();
// 設定三條邊,找出異常
try {
t1.setEdgea(4);
t1.setEdgeb(5);
t1.setEdgec(3);
t1.Checking(t1.getEdgea(), t1.getEdgeb(), t1.getEdgec());
} catch (EdgeTooSmallException e) {
System.out.println(e.getInfo());
System.exit(-1);
} catch (EdgeNotMatchException e) {
System.out.println(e.getInfo());
System.exit(-1);
}
System.out.println("邊長爲" + t1.getEdgea() + "," + t1.getEdgeb() + ","
+ t1.getEdgec());
}
}
3.Person類中增加birthday屬性,對setBirthday(int ,int , int )方法進行異常處理,
要求年有效、月有效、日有效、年月日指定的具體日期有效,對不同情況分別拋出不同的異常。
答:
/**
* Person類中增加birthday屬性,對setBirthday(int ,int , int )方法進行異常處理,
* 要求年有效、月有效、日有效、年月日指定的具體日期有效,對不同情況分別拋出不同的異常。
*
*/
public class Brithday {
private String birthday;
public String getBirthday() {
return birthday;
}
public void setBirthday(int year, int month, int date)
throws InvalidParamExcetion {
// 判斷年份
if (year < 1900 || year > 2016) {
throw new InvalidParamExcetion("年份不合適,請傳入1900年到2016年之間的年份");
}
// 判斷月份
if (month <= 0 || month > 12) {
throw new InvalidParamExcetion("月份不合適,不存在" + month + "月");
}
// 判斷日期
if (date > 31 || date <= 0) {
throw new InvalidParamExcetion("日期不合適,不存在" + date + "日");
}
boolean isThirdOne = date == 1 || date == 3 || date == 5 || date == 7
|| date == 8 || date == 10 || date == 12;
if (!isThirdOne && date == 31) {
throw new InvalidParamExcetion("日期不合適," + month + "月不存在" + date
+ "日");
}
if (month == 2 && date > 29) {
throw new InvalidParamExcetion("日期不合適,2月不存在" + date + "日");
}
if (year % 4 != 0 && month == 2 && date == 29) {
throw new InvalidParamExcetion("日期不合適," + year + "不是閏年,所以2月不存在"
+ date + "日");
}
System.out.println("生日爲:" + year + "年" + month + "月 " + date + "日 ");
}
public static void main(String[] args) throws InvalidParamExcetion {
Brithday person = new Brithday();
try {
person.setBirthday(2015, 12, 5);
person.setBirthday(2016, 2, 29);
person.setBirthday(2015, 2, 29);
// 因爲上一句異常了,所以不會執行這一句
person.setBirthday(2015, 3, 5);
} catch (InvalidParamExcetion e) {
}
}
}
/**
* 非法參數異常
*
*/
class InvalidParamExcetion extends Exception {
public InvalidParamExcetion(String msg) {
super(msg);
System.out.println(msg);
}
}
4.將類定義到指定的包下。com.it18zhang,編譯之後,打成jar文件。
答:
打包java庫文件。
[將類路徑下的類打成jar文件]
jar cvf myjar.jar -C classes/ .
jar cvfe myjar.jar com.it18zhang.A -C classes/ .//e指定的入口點.
[使用java -jar參數來運行程序]
java -jar myjar.jar//執行jar文件
java -jar myjar.jarcom.it18zhang.A//執行jar文件指定入口點。
5.相互之間使用jar包,放置cp下,對class進行重用。
答:答案略
6.設計程序,考查修飾符。public -> protected -> default -> private(選做題)
答:
package com.it18zhang.job.demo;
public class Person {
protected String protectName;
public void method_public() {
System.out.println("Person method_public");
}
protected void method_protect(){
System.out.println("Person method_protect");
}
void method_defult(){
System.out.println("Person method_defult");
}
private void method_private(){
System.out.println("Person method_private");
}
}
package com.it18zhang.job.demo;
public class People {
public static void main(String[] args) {
System.out.println("同包中的類修飾符權限測試");
Person person = new Person();
person.method_public();
person.method_protect();
person.method_defult();
//無法調用private
//person.method_private();
}
}
package com.it18zhang.job.demo2;
import com.it18zhang.job.demo.Person;
public class Student extends Person{
public static void main(String[] args) {
System.out.println("子類修飾符權限測試");
Student student = new Student();
student.method_public();
student.method_protect();
//無法調用defult、private
//student.method_private();
//student.method_defult();
}
}
package com.it18zhang.job.demo2;
import com.it18zhang.job.demo.Person;
public class Other {
public static void main(String[] args) {
System.out.println("其他包中類修飾符權限測試");
Person person = new Person();
person.method_public();
//無法調用其他包中類的protect,default和private方法
//person.method_protect();
//person.method_defult();
//person.method_private();
}
}
7.預習多線程。