Java---复习

  快期末考试了,总结一下Java的知识点吧!话说,好像好久不写博客了,趁着考试多来几篇。。。

1. Java中的方法重写

01 当且仅当实例方法是可以访问的,该方法才能被覆盖。

因为私有方法在类外是不可以被访问的,所以他不能被覆盖。(不能被覆盖自然也不满足多态)如果说子类中定义的方法在父类中是私有的,那么这俩方法完全没关系。

 public class Test {
      public static void main(String[] args) {
        new Person().printPerson();
        new Student().printPerson();
      }
    }

    class Student extends Person {
      private String getInfo() { //虽然和父类中的getinfo一样,但不属于方法重写,不满足多态,父类的引用指向子类对象时,调用printPerson,printPerson仍然调用父类中的getinfo
        return "Student";
      }
    }

    class Person {
      private String getInfo() {
        return "Person";
      }

      public void printPerson() {
        System.out.println(getInfo());
      }
    }
//输出: Person
//      Person

 

 02 java中静态方法不能被重写(覆盖)

静态方法可以被继承,但是,不能被覆盖,即重写。如果父类中定义的静态方法在子类中被重新定义,那么在父类中定义的静态方法将被隐藏。可以使用语法:父类名.静态方法调用隐藏的静态方法。 
如果父类中含有一个静态方法,且在子类中也含有一个返回类型、方法名、参数列表均与之相同的静态方法,那么该子类实际上只是将父类中的该同名方法进行了隐藏,而非重写。换句话说,父类和子类中含有的其实是两个没有关系的方法,它们的行为也并不具有多态性 
因此,通过一个指向子类对象的父类引用变量来调用父子同名的静态方法时,只会调用父类的静态方法。

例题:

package test;

public class Test_2 {
    public static void main(String[] args) {
      new A();
      new B();
    }
  }

  class A {
    int i = 7;

    public A() {
      setI(20);
      System.out.println("i from A is " + i);
    }

    public void setI(int i) {
      this.i = 2 * i;
    }
  }

  class B extends A {
    public B() {
      System.out.println("i from B is " + i);    
    }

    public void setI(int i) {
      this.i = 3 * i;
      System.out.print("调用子类的setI\n");
    }
  }
output:
i from A is 40
调用子类的setI
i from A is 60
i from B is 60



   在new B()的时候,会先调用父类A的构造方法,A根据多态的特性,会调用子类B中重写的setI方法。

 

对象转换和instanceof运算符

例题:

 

Fruit fruit = new GoldenDelicious();
    Orange orange = new Orange();

 

a.	Is fruit instanceof Fruit? 是
b.	Is fruit instanceof Orange? 否
c.	Is fruit instanceof Apple? 是
d.	Is fruit instanceof GoldenDelicious? 是
e.	Is fruit instanceof McIntosh? 否
f.	Is orange instanceof Orange? 是
g.	Is orange instanceof Fruit? 是
h.	Is orange instanceof Apple? 否
i.	Suppose the method makeAppleCider is defined in the Apple class. Can fruit invoke this method?  是  Can orange invoke this method?  否
j.	Suppose the method makeOrangeJuice is defined in the Orange class. Can orange invoke this method? 是 Can fruit invoke this method?  否
k.	Is the statement Orange p = new Apple() legal?  否
l.	Is the statement McIntosh p = new Apple() legal? 否
m.	Is the statement Apple p = new McIntosh() legal? 是

例题2:(很好的一个题目,)

题目要求:When overriding the equals method, a common mistake is mistyping its signature in the subclass. For example, the equals method is incorrectly written as equals(Circle circle), as shown in (a) in following the code; instead, it should be equals(Object circle), as shown in (b). Show the output of running class Test with the Circle class in (a) and in (b), respectively.

  public class Test {
      public static void main(String[] args) {
        Object circle1 = new Circle();
        Object circle2 = new Circle();
        System.out.println(circle1.equals(circle2));
      }
    }

    (a)
    class Circle {
      double radius;

      public boolean equals(Circle circle) {
        return this.radius == circle.radius;    
      }
    }

    (b)
    class Circle {
      double radius;

      public boolean equals(Object circle) {
        return this.radius == 
          ((Circle)circle).radius;    
      }
    }

If Object is replaced by Circle in the Test class, what would be the output to run Test using the Circle class in (a) and (b), respectively?

The output is false if the Circle class in (a) is used. The Circle class has two overloaded methods: equals(Circle circle) defined in the Circle class and equals(Object circle) defined in the Object class, inherited by the Circle class. At compilation time, circle1.equals(circle2) is matched to equals(Object circle), because the declared type for circle1 and circle2 is Object. (Note that either the declared type for circle1 and circle2 is Object would cause circle1.equals(circle2) to match circle1.equals(Object circle) by the compiler.The output is true if the Circle class in (b) is used. The Circle class overrides the equals(Object circle) method defined in the Object class. At compilation time, circle1.equals(circle2) is matched to equals(Object circle) and at runtime the equals(Object circle) method implemented in the Circle class is invoked. 

In (a), method equals(Object c) is used at both compilation time and run time. circle1 and circle2 have different addresses, leading to "false" output. Method overriding follows dynamic binding (determined by the actual type), but method overloading is always determined by the declared type. 

What would be the output if Object is replaced by Circle in the Test class using the Circle class in (a) and (b), respectively?

The output would be true for (a), because circle1.equals(circle2) matches circle1.equals(Circle object) exactly in this case. The output would be true for (b) because equals(Object c) is overridden in the Circle class.

 

 

 

 

 

 

 

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值