Java第五次课程作业

  1. 如何实现两个对象之间互发消息,请举例说明。
    通过对象引用的方法,创建两个类,这两个类中都包含另一个类的成员。
eg:class A{
  private B b;
  public void setB(B *b){
  if(*b!-null){b=*b}
  }
  public B getB(){
  if(b!=null){return b}
  else return null;
  }
}
class B{
  A a;
  public B(A aa){
  this.a=aa;
  aa.setB(this);
  }
}
  1. 谈谈组合与继承的区别以及两者的使用场景(即什么时候宜用组合 ?什么时候宜用继承?)。
    继承是子类继承父类,父类的所有属性和方法都可以被子类访问和调用。组合是指将已存在的类型作为一个新建类的成员变量类型,两个类之间无上下级关系。即只需要使用另一个类的方法时用组合,当你需要使用另外一个类时又不想被其他类访问时用继承。
  2. Java中的运行时多态的含义是什么?有什么作用?请举例说明。
    运行时多态指的是:当同一个引用变量(父类引用)指向不同的子类实例,然后访问引用变量成员方法, 方法会有不同的表现。
    作用:提供了更大的灵活性,因为所有事情都在运行时得到解决。
eg:interface InterA  
{  
    void fun();  
}  
class B implements InterA  
{  
    public void fun()  
    {      
        System.out.println(“This is B”);  
    }  
}  
class C implements InterA  
{  
    public void fun()  
    {      
        System.out.println(“This is C”);  
    }  
}  

class Test  
{  
    public static void main(String[] args)  
    {  
        InterA  a;
        a= new B();  
        a.fun();   
        a = new C();   
        a.fun();   
    }  
}
  1. 使用接口改写例6.8中的程序。
package Interface;
import java.applet.Applet;
import java.awt.*;
interface Shape {
    public abstract double getArea();
    public abstract double getPerimeter();
}
class Rect implements Shape {
    public int x, y, k;
    public double m;
    public Rect(int x,int y,int k,double m) {
        this.x = x;
        this.y = y;
        this.k = k;
        this.m = m;
    }
    public double getArea() {
        return (k*m);
    }
    public double getPerimeter() {
        return (2*(x+y));
    }
}
class Triangle implements Shape {
    public int a, b, c;
    public double m;
    public Triangle(int a,int b,int c) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.m = (a + b + c)/2.0;
    }
    public double getArea() {
        return (Math.sqrt(m*(m-a)*(m-b)*(m-c)));
    }
    public double getPerimeter() {
        return (a+b+c);
    }

}
class Circle implements Shape {
    public int x, y, d;
    public double r;
    public Circle(int x,int y,int d) {
        this.x = x;
        this.y = y;
        this.d = d;
        this.r = d/2.0;
    }
    public double getArea() {
        return (Math.PI*r*r);
    }
    public double getPerimeter() {
        return (2*Math.PI*r);
    }
}
class RunShape extends Applet {
    Rect rect = new Rect(5,15,25,25);
    Triangle tri = new Triangle(5,5,8);
    Circle cir = new Circle(13,90,25);
    public void init(){
    }
    private void drawArea(Graphics g,Shape s,int a,int b) {
        g.drawString(s.getClass().getName()+" Area"+s.getArea(),a,b);
    }
    private void drawPerimeter (Graphics g,Shape s,int a,int b) {
        g.drawString(s.getClass().getName()+" Perimeter"+s.getPerimeter(),a,b);
    }
    public void paint(Graphics g) {
        g.drawRect(rect.x,rect.y,rect.k,(int)rect.m);
        g.drawString("Rect Area:"+rect.getArea(),50,35);
        g.drawString("Rect Perimeter:"+rect.getPerimeter(),50,55);
        g.drawString("Triangle Area:"+tri.getArea(),50,75);
        g.drawString("Triangle Perimeter:"+tri.getPerimeter(),50,95);
        g.drawOval(cir.x-(int)cir.d/2,cir.y-(int)cir.d/2,cir.d,cir.d);
        g.drawString("Circle Area:"+cir.getArea(),50,115);
        g.drawString("Circle Perimeter:"+cir. getPerimeter(),50,135);
        }
}
  1. 自定义一个类,覆写equals方法,以满足自身业务需求
class Person{
    private String name;
    private int age;
    public Person(String name,int age){
        this.name=name;
        this.age=age;
    }
    public String toString(){
        return this.name+"今年"+this.age+"岁";
    }
    public boolean equals(Object obj){
        if(obj==null){NullPointerExceptionreturn false;
        }
        if(this==obj){
            return true;
        }
        if(!(obj instanceof Person)){
            return false;
        }
        Person per=(Person)obj;        return this.name.equals(per.name)&&this.age==per.age;
    }
}
class Student{}
public class Test{
    public static void main(String[] args) {
       Person per1=new Person("张三",18);
       Person per2=new Person("张三",18);
       Person per3=new Person("lisi",19);
       Person per4=null;
       Student stu=new Student();
       System.out.println(per1.equals(per1));//true
       System.out.println(per1.equals(stu));//false
       System.out.println(per1.equals(per3));//false
       System.out.println(per1.equals(per4));//false
    }
}
  1. 举例说明运算符instanceof的使用场景。
    instanceof是一个二元运算符,用来判断对象是否为特定类的实例。
    eg:class A{…}
    A a=new A();
    boolean B=a instance of A;
  2. 谈谈抽象类与接口的异同以及两者的使用场景。
    抽象类可以提供成员方法的实现细节,而接口中只能存在public abstract 方法;抽象类中的成员变量可以是各种类型的,而接口中的成员变量只能是public static final类型的;抽象类可以有静态代码块和静态方法,而接口中不能含有静态代码块以及静态方法;一个类只能继承一个抽象类,而一个类却可以实现多个接口。
    使用场景:拥有一些方法并且想让它们中的一些有默认实现,实现多重继承,那么你必须使用接口。因为Java不支持多继承,子类不能够继承多个类,但可以实现多个接口。因此可以使用接口来解决它。如果基本功能在不断改变,那么就需要使用抽象类。如果不断改变基本功能并且使用接口,那么就需要改变所有实现了该接口的类。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值