利用白富美接口案例,土豪征婚使用匿名内部类对象实现。
interface Iwhite { public void white(); } interface Irich { public void rich(); } interface Ibeauti { public void beauti(); } interface WRB extends Iwhite,Irich,Ibeauti { } class WomenStar implements WRB { public void white(){ System.out.println("白"); } public void rich(){ System.out.println("富"); } public void beauti(){ System.out.println("美"); } } class RichMan7 { public void marry(WomenStar w) { System.out.println("结婚"); } } class MarryDemoNew { public static void main(String[] args){ RichMan7 m=new RichMan7(); m.marry(new WomenStar(){ public void white(){ System.out.println("白"); } public void rich(){ System.out.println("富"); } public void beauti(){ System.out.println("美"); } }); } }
2.定义三角形类Trianle,里面包含三个int类型属性,分别表示三条边的长度,
构造三角形对象时,任意两边之和是否大于第三边,如若不成立,抛出自定义异常。
package it18zhang.com;
class EdgeTooSmallException extends Exception{
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;
public int getEdgea(){
return edgea;
}
public int getEdgeb() {
return edgeb;
}
public int getEdgec() {
return edgec;
}
public void setEdgea(int edgea) throws EdgeTooSmallException{
if(edgea>0){
this.edgea=edgea;
}
else{
throw new EdgeTooSmallException("边长a不能小于等于0");
}
}
public void setEdgeb(int edgeb) throws EdgeTooSmallException{
if(edgeb>0){
this.edgeb=edgeb;
}
else{
throw new EdgeTooSmallException("边长b不能小于等于0");
}
}
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("三角形成立");
}
}
}
public class TrianleDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Triangle t=new Triangle();
try{
t.setEdgea(4);
t.setEdgeb(5);
t.setEdgec(3);
t.Checking(t.getEdgea(), t.getEdgeb(), t.getEdgec());
}
catch (EdgeTooSmallException e){
System.out.println();
System.exit(-1);
} catch (EdgeNotMatchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(-1);
}
}
}
3.Person类中增加birthday属性,对setBirthday(int ,int , int )方法进行异常处理,
要求年有效、月有效、日有效、年月日指定的具体日期有效,对不同情况分别抛出不同的异常。
package it18zhang.com;
public class Birthday{
private String birthday;
public String getBirthday(){
return birthday;
}
public void setBirthday(int year,int month,int day) throws Exception{
if(year<1900||year>2016){
throw new Exception("年份不对");
}
if(month<=0||month>12){
throw new Exception("月份不对");
}
if(day>31||day<0){
throw new Exception("日期不对");
}
//判断具体日期
boolean isBigMonth=(month==1||month==3||month==5||month==7||month==8||month==10||month==12);
if(!isBigMonth&&day==31){
throw new Exception("本月不可能有31天");
}
if(month==2&&day==29){
throw new Exception("2月没有29天");
}
//判断是否是闰月
if(year % 4 != 0 && month == 2 && day == 29){
throw new Exception("不是润月没有29天");
}
System.out.println("_________");
}
public static void main(String[] args) throws Exception {
Birthday person = new Birthday();
try {
person.setBirthday(2015, 12, 5);
person.setBirthday(2016, 2, 29);
person.setBirthday(2015, 2, 29);
// 因为上一句异常了,所以不会执行这一句
person.setBirthday(2015, 3, 5);
} catch (Exception e) {
}
}
}
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.预习多线程。