Java第5章:面向对象(中)

it2025-02-28  25

1. Java的访问控制修饰符有哪些?各有什么访问权限?请对照第7页ppt的表格分别写程序验证。

控制修饰符及访问权限:

访问权限首先取决于类前的修饰符。如果类A要访问类B的方法,前提条件是类A必须具有访问类B的权限。当类B前的修饰符为public时,则类B可以被所有类访问—即import;当为默认时,则只能为包中的类所访问。 在类能被访问的前提下,再看类方法前的修饰符。现以类前修饰符public为例来进行说明。当类的属性和方法前的修饰符为public时,则该属性和方法可以被所有类访问;当属性和方法前的修饰符为protected时,在类定义层面上,访问权限为B+C,而在对象层面上,则为B;当属性和方法前的修饰符为默认时,访问权限只限于b;当属性和方法前的修饰符为private时,只能被本类内部的方法访问。

验证如下: (1)在以下示例中,FighterPlane的两个属性修饰符为public;另一个包中的类RunPane才能够访问FighterPlane对象的属性;如果FighterPlane的两个修饰符为protected,则以下程序编译不会通过,会提示没有访问name或missileNum的权限。

package com.resource;//声明在某个包中 public class FighterPlane{ public String name; public int missileNum; public void fire(){ if(missileNum > 0){ System.out.println("now fire a missile !"); missileNum -= 1; } else{ System.out.println("No missile left !"); } } } package com.run; import com.resource. * ; public class RunPlane{ public static void main(Strings args[]){ FighterPlane fp = new FighterPlane(); fp.name = "苏35"; fp.missileNum = 6; fp.fire(); } }

(2)在以下示例中,FighterPlane类的静态属性修饰符为public,另一个包中的类RunPlane才能访问FighterPlane中的静态属性;如果修饰符为protected或private,则以下示例程序编译不会通过,会提示没有访问name的权限。

package com.resource; public class Fighterplane { public static String name = "苏35"; } package com.run; import com.resource. * ; public class RunPlane{ public static void main(String args[]){ System.out.println(FighterPlane.name); } }

2. 子类对于从父类继承的哪些属性与方法是可见的?请分别写程序进行验证。

子类继承了父类的所有属性和方法, 但只有public、protected的属性和方法在子类中是可见的,private修饰的属性和方法对子类不可见。如:

class FatherPlane{ public String namePub = "Public name"; protected String namePro = "Protected name"; private String namePri = "Private name"; } class SonPlane extends FatherPlane{ public void setName(String name_){ this.namePri = "new private"; //The field FatherPlane.namePri is not visible } } public class RunPlane{ public static void main(String args[]) { SonPlane sp = new SonPlane(); System.out.println(sp.namePub); // 无报错 System.out.println(sp.namePro); // 无报错 System.out.println(sp.namePri); // The field FatherPlane.namePri is not visible } }

3. 什么是组合?有什么作用?请举例说明。

组合的概念 在一个类里面使用另一个类对象的引用,并通过发消息的形式调用该对象的属性和方法。

组合的作用 使不同对象之间的耦合比较松散,独立性较强,可重用性较高。

举例如下:

class FighterPlane { String name; int missileNum; public FighterPlane (String _name, int _missileNum) { name = _name; missileNum = _missileNum; } public void fire() { if (missileNum > 0) { System.out.println("now fire a missile !"); missileNum -= 1; } else{ System.out.println("No missile left !"); } } } class A { FighterPlane fp; public A(FighterPlane fpp) { this.fp = fpp; //A对象中拥有了FighterPlane对象的引用 } public void invoke() { //A对象发送消息给FighterPlane的对象 System.out.println(fp.name); } } public class Run { public static void main(String[] args) { FighterPlane ftp = new FighterPlane ("su35",10); //产生A对象,并将ftp作为对象引用传入 A a = new A(ftp); //发送消息,产生调用关系 a.invoke(); } }

4. 什么是重载?有什么作用?请举例说明。

重载的概念 在类中定义了多个同名但不同内容参数的成员方法时,称这些方法时重载(overloading)方法。重载的作用 使得同一个方法处理不同类型的参数并返回不同类型的值,虽然参数不同但这些同名方法所实现的作用是相同的,是实现多态的非常重要的机制。举例如下: class Parent { public int getScore() { return 3; } public int getScore(int i) { return i; } }

5. 什么是覆盖?有什么作用?请举例说明。

覆盖的概念 子类对父类参数相同、返回类型相同的同名方法重新进行定义,这种多态被称为覆盖(overriding).覆盖的作用 使得子类能够灵活的根据自己的需要修改父类中所定义的方法,而无需修改父类中的实现。举例如下 class Parent { public int getScore() { return 3; } public String getCountryName() { return "China"; } } class Son extends Parent { public int getScore() { return 4; } } public class RunSon { public static void main(String args[]){ Son s = new Son(); System.out.println(s.getScore()); System.out.println(s.getCountryName()); } } /****** 输出结果: 4 China ******/
最新回复(0)