JavaDay08--Homework(定义接口和类并调用属性和方法+对象的多态性+打印对象+判定对象相等)

一、定义以下这些接口和类,并完成相关属性和方法的声明和调用。(数据类型,参数列表,返回值类型等根据题目需要自行定义)

(1)学习接口Learning(包含方法:预习preLearn,上课lessons,复习reveiw)

(2)喝酒接口Drinking(包含方法:喝酒drink,吐throwUp,耍酒疯playMad)

(3)人抽象类Person(包含属性:姓名,性别,年龄;抽象方法:谈恋爱love)

学生Student是人,会学习,不能喝酒(因为会使大脑变笨),有自己的学校(school),喜欢和朋友聊微信(chatting)。

公务员Officer是人,不用学习,需要喝酒(经常应酬),经常开一些无聊的会议(meeting)。

程序猿Programmer,是人,经常学习,不喝酒(社交较少),喜欢写代码(coding)和修bug(debuging)。

 

package behaviordemo;

public interface Learning {
	public void preLearn();
	
	public void lessons();
	
	public void reveiw();
	
}
package behaviordemo;

public interface Drinking {
	public void drink();

	public void throwUp();

	public void playMad();

}
package behaviordemo;

public class TestLearning {

	public void operLearn(Learning l) {
		l.preLearn();
		l.lessons();
		l.reveiw();
	}

}
package behaviordemo;

public class TestDrinking {

	public void operDrink(Drinking d) {
		d.drink();
		d.throwUp();
		d.playMad();
	}

}
package behaviordemo;

public class Person {

	private String name;
	private String sex;
	private int age;

	public Person() {
	}

	public Person(String name, String sex, int age) {
		this.name = name;
		this.sex = sex;
		this.age = age;
		System.out.println("此人姓名为" + name + ",性别为" + sex + ",年龄为" + age);
	}

	public void love(String name) {
		System.out.println(name + "会谈恋爱...");
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}
package behaviordemo;

public class Student extends Person implements Learning {

	public Student() {
	}

	public void preLearn() {
		System.out.println("学生学习需要预习...");
	}

	public void lessons() {
		System.out.println("学生学习需要上课...");
	}

	public void reveiw() {
		System.out.println("学生学习需要复习...");
	}

	public void school() {
		System.out.println("学生有自己的学校...");
	}

	public void chatting() {
		System.out.println("学生喜欢和朋友聊微信...");
	}

}

package behaviordemo;

public class Officer extends Person implements Drinking {

	public Officer() {
	}

	public void drink() {
		System.out.println("公务员应酬需要喝酒...");
	}

	public void throwUp() {
		System.out.println("公务员喝酒会吐...");
	}

	public void playMad() {
		System.out.println("公务员喝酒会耍酒疯...");
	}

	public void meeting() {
		System.out.println("公务员要开无聊的会议...");
	}

}
package behaviordemo;

public class Programmer extends Person implements Learning {

	public Programmer() {
	}

	public void preLearn() {
		System.out.println("程序猿学习需要预习...");
	}

	public void lessons() {
		System.out.println("程序猿学习需要上课...");
	}

	public void reveiw() {
		System.out.println("程序猿学习需要复习...");
	}

	public void coding() {
		System.out.println("程序猿特别喜欢写代码...");
	}

	public void debuging() {
		System.out.println("程序猿特别喜欢修bug...");
	}
}
package behaviordemo;

public class Test {

	public static void main(String[] args) {
		TestLearning tl = new TestLearning();
		TestDrinking td = new TestDrinking();
		Student stu = new Student(); // 实例化学生、公务员、程序猿对象,并向上转型
		Officer off = new Officer();
		Programmer pro = new Programmer();
		stu.love("学生");
		tl.operLearn(stu);
		stu.school();
		stu.chatting();
		System.out.println();
		off.love("公务员");
		td.operDrink(off);
		off.drink();
		off.playMad();
		off.throwUp();
		System.out.println();
		pro.love("程序猿");
		tl.operLearn(pro);
		pro.coding();
		pro.debuging();
	}

}


二、在场景类Client中定义一个方法method1,在形参和实参上体现对象的多态性,在方法中进行调用。

如果对象的实际类型是学生,就和朋友聊微信;

如果是公务员,就去开会;

如果是程序猿,就去写代码和修bug。

package client;

public class Client {
	public Client() {
	}
}

package client;

public class Student extends Client {
	public void chatting() {
		System.out.println("学和朋友聊微信。");
	}
}
package client;

public class Officer extends Client {
	public void meeting() {
		System.out.println("公务员开会。");
	}
}
package client;

public class Programmer extends Client {
	public void coding() {
		System.out.println("程序员写代码和修bug。");
	}

}

package client;

public class TestClient {

	public static void main(String[] args) {
		Student stu = new Student();
		Officer off = new Officer();
		Programmer pro = new Programmer();
		method1(stu);
		method1(off);
		method1(pro);
	}

	public static void method1(Client cli) {
		if (cli == null) {
			System.out.println("不能直接传递null!");
			return;
		}
		if (cli instanceof Student) {
			Student stu = (Student) cli;
			stu.chatting();
		}
		if (cli instanceof Programmer) {
			Programmer pro = (Programmer) cli;
			pro.coding();
		}

		if (cli instanceof Officer) {
			Officer off = (Officer) cli;
			off.meeting();
		}

	}

}




三、直接打印一个学生对象,就能以下面格式来输出:

学生信息:  姓名:张三,性别:男,年龄:20,学校:北大)

package inner.print;

public class Student {

	// 成员内部类
	class Inner {
		public void method() {
			System.out.println("学生信息:  姓名:张三,性别:男,年龄:20,学校:北大.");
		}
	}

	public Inner get() {
		return new Inner();
	}
}
package inner.print;

public class TestPrint {

	public static void main(String[] args) {
		// 方法一
		Student stu = new Student();
		stu.get().method(); // 通过在外部类中的成员方法中获取内部类对象
		/*
		 * 方法二 
		 * Student.Inner in=stu.new Inner(); // 外部类.内部类 内部类对象=外部类实例.new
		 * 内部类(); in.method();
		 */
	}

}



四、如果两个学生的姓名、性别、年龄、学校一样,则认为这两个学生“相等”。

package equal;

public class Student {
	private String name;
	private String sex;
	private int age;
	private String school;

	public Student() {

	}

	public Student(String name, String sex, int age, String school) {
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.school = school;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			System.out.println("参数为null,不相等");
			return false;
		}
		if (!(obj instanceof Student)) { // 传递的参数是否属于Student类
			System.out.println("传递的不是Student对象");
			return false;
		}
		Student stu = (Student) obj; // 向下转型
		if (this.name.equals(stu.name) && this.sex.equals(stu.sex)
				&& this.age == stu.age && this.school.equals(stu.school)) {
			return true;
		}
		return false;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}

}
package equal;

public class TestEqual {

	public static void main(String[] args) {
		Student stu1 = new Student("张三", "男", 20, "西邮");
		Student stu2 = new Student("李四", "男", 20, "西邮");
		boolean flag = stu1.equals(stu2);
		System.out.println(flag ? "学生1和学生2“相等”" : "学生1和学生2“不相等”");
	}

}



Copyright © 2017 Jin Hanquan. All rights reserved.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值