精华内容
下载资源
问答
  • Homework

    2019-10-25 10:40:51
    Homework
  • homework

    2017-12-25 17:46:01
    homework
    (1)定义以下这些接口和类,并完成相关属性和方法的声明和调用.数据类型,参数列表,返回值类型等请根据题目需要自行定义.
    学习接口Learning(包含方法: 预习preLearn,上课lessons,复习reveiw)
    喝酒接口Drinking(包含方法: 喝酒drink, 吐throwup,耍酒疯playMad)
    抽象类Person(包含属性:姓名、年龄、性别,抽象方法:谈恋爱(Love) 
    学生Student是人,得会学习但不能喝酒(因为会使大脑变笨);他们还有自己的学校(school),还喜欢和朋友聊微信(chatting).
    公务员Officer是人,不用学习,但经常需要喝酒应酬;他们还得经常开一些无聊的会议(meeting).
    程序猿Programmer,是人,必须经常学习,较少社交所以不喝酒;他们特别喜欢写代码(coding),和修bug(debuging).
    (2)在场景类Client中定义一个方法method1,在形参和实参上体现对象的多态性,在方法中进行调用,如果对象的实际类型是学生,就和朋友聊微信;如果是公务员,就去开会;如果是程序猿,就去写代码和修bug.
    (3)直接打印一个学生对象,就能以下面格式来输出:
    学生信息:  姓名:张三,性别:男,年龄:20,学校:北大.
    (4)如果两个学生的姓名、性别、年龄、学校一样,则认为这两个学生“相等”。

    学习接口Learning:
    package homework;
    
    public interface Learning {
    	public abstract void preLearn();
    	public abstract void lessons();
    	public abstract void review();
    }
    

    喝酒接口Drinking:
    package homework;
    
    public interface Drinking {
    	public abstract void drink();
    	public abstract void throwUp();
    	public abstract void playMad();
    }
    

    抽象类Person:
    package homework;
    public abstract class Person {
    	private String name;
    	private String sex;
    	private int age;
    	
    	public Person() {
    		super();
    	}
    
    	public Person(String name, String sex, int age) {
    		super();
    		this.name = name;
    		this.sex = sex;
    		this.age = age;
    	}
    
    	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 abstract void love();
    	public void work(){
    		
    	}
    }
    

    学生类Student继承Person类实现Learning接口:
    package homework;
    
    public class Student extends Person implements Learning{
    	private String school;
    	public Student() {
    	}
    	public Student(String name,String sex,int age, String school){
    		super(name, sex, age);
    		this.school=school;
    	}
    	
    	public String getSchool() {
    		return school;
    	}
    	public void setSchool(String school) {
    		this.school = school;
    	}
    	@Override
    	public void love() {
    
    	}
    
    	@Override
    	public void preLearn() {
    
    	}
    
    	@Override
    	public void lessons() {
    		
    	}
    
    	@Override
    	public void review() {
    		
    	}
    	public void chatting(){
    		System.out.println("学生和朋友聊微信");
    	}
    	@Override
    	public String toString() {
    		return "姓名:"+getName()+",性别:"+getSex()+",年龄:"+getAge()+",学校:"+getSchool();
    	}
    	@Override
    	public boolean equals(Object object) {
    		if(object==null){
    			return false;
    		}else if(object==this){
    			return true;
    		}else if(!(object instanceof Student)){
    			return false;
    		}
    		Student student=(Student)object;
    		if(getName().equals(student.getName()) && getSex().equals(student.getSex()) && getAge()==student.getAge() && school.equals(student.school)){
    			return true;
    		}
    		return false;
    	}
    }
    

    公务员Officer继承Person类实现Drinking接口:
    package homework;
    
    public class Officer extends Person implements Drinking{
    
    	@Override
    	public void drink() {
    	
    	}
    
    	@Override
    	public void throwUp() {
    	
    	}
    
    	@Override
    	public void playMad() {
    	
    	}
    
    	@Override
    	public void love() {
    		
    	}
    	public void meeting(){
    		System.out.println("公务员去开会");
    	}
    }
    

    程序猿Programmer类继承Person类实现Learning接口:
    package homework;
    public class Programmer extends Person implements Learning{
    
    	@Override
    	public void preLearn() {
    	}
    
    	@Override
    	public void lessons() {
    	}
    
    	@Override
    	public void review() {
    	}
    
    	@Override
    	public void love() {
    	}
    	public void coding(){
    		System.out.println("程序员写代码");
    	}
    	public void debuging(){
    		System.out.println("程序员修bug");
    	}
    }
    

    场景类Client:
    package homework;
    
    public class Client {
    	public void method1(Person person){
    		if(person==null){
    		System.out.println("不能传空值");	
    		}else if(person instanceof Student){
    			Student student=(Student)person;
    			student.chatting();
    		}else if(person instanceof Programmer){
    			Programmer programmer=(Programmer)person;
    			programmer.coding();
    			programmer.debuging();
    		}else if(person instanceof Officer){
    			Officer officer=new Officer();
    			officer.meeting();
    		}
    	}
    }
    
    测试类Test:
    package homework;
    
    public class Test {
    	public static void main(String[] args){
    		Student student1=new Student();
    		Client client=new Client();
    		//学生和朋友聊天
    		client.method1(student1);
    		Person programmer=new Programmer();//向上转型
    		//程序员改代码、修bug
    		client.method1(programmer);
    		//公务员开会
    		client.method1(new Officer());
    		
    		Student student=new Student("张三","男",20,"北大");
    		System.out.println(student);
    		Student student2=new Student("张三","男",20,"北大");
    		System.out.println(student.equals(student2)?"学生相等":"学生不相等");
    		
    	}
    	
    }
    




    展开全文
  • homework-源码

    2021-03-31 17:30:34
    homework
  • HomeWork-源码

    2021-03-31 02:30:53
    HomeWork
  • Homework-源码

    2021-03-29 05:37:21
    Homework
  • HOMEWORK-源码

    2021-03-22 18:07:28
    HOMEWORK

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 16,128
精华内容 6,451
关键字:

homework