如何使用修饰符控制继承?
How do I control Inheritance with modifiers?
我基本上是在寻找一种方法来使用修饰符和方法主体中的一些额外行来修改以下源代码,因此它会在我的控制台中打印出以下内容:
1g
1hb
2f
1g
2hb
1hb
这是我大学 classes 的练习,我似乎无法理解它。我只允许更改除 println 行之外的方法主体以及更改方法的修饰符。我应该怎么做,修饰符在继承方面有什么关系?如何重载方法以获得所需的结果?
这是我的主要方法:
public class Poly {
public static void main( String args[] ) {
Poly1 a = new Poly1();
a.g();
Poly2 b = new Poly2();
b.f();
}
}
这是我的第一个 class:
public class Poly1 {
public void f() {
System.out.println( "1f" );
g();
}
private void g() {
System.out.println( "1g" );
h( 10 );
}
protected void h( int i ) {
System.out.println( "1hi" );
}
void h( byte b ) {
System.out.println( "1hb" );
}
}
下面是我的第二个 class:
public class Poly2 extends Poly1 {
protected void f() {
System.out.println( "2f" );
Poly1 c=new Poly1();
g();
h();
}
public void g() {
System.out.println( "2g" );
h( 18 );
}
public void h( int i) {
System.out.println( "2hi" );
}
public void h( byte b ) {
System.out.println( "2hb" );
}
}
public class Poly1 {
public void f() {
System.out.println("1f");
g();
}
public void g() {
System.out.println("1g");
h((byte) 10); // cast to byte to invoke the overloaded method void
// h(byte b)
}
protected void h(int i) {
System.out.println("1hi");
}
void h(byte b) {
System.out.println("1hb");
}
}
public class Poly2 extends Poly1 {
public void f() { //change from protected to public since the visibility of an overidden method Cannot be reduced
System.out.println("2f");
Poly1 c = new Poly1();
c.g(); // invoke the methode g of Poly1
h((byte) 10);
}
public void g() {
System.out.println("2g");
h(18);
}
protected void h(int i) {
System.out.println("2hi");
}
public void h(byte b) {
System.out.println("2hb");
}
}
我基本上是在寻找一种方法来使用修饰符和方法主体中的一些额外行来修改以下源代码,因此它会在我的控制台中打印出以下内容:
1g
1hb
2f
1g
2hb
1hb
这是我大学 classes 的练习,我似乎无法理解它。我只允许更改除 println 行之外的方法主体以及更改方法的修饰符。我应该怎么做,修饰符在继承方面有什么关系?如何重载方法以获得所需的结果?
这是我的主要方法:
public class Poly {
public static void main( String args[] ) {
Poly1 a = new Poly1();
a.g();
Poly2 b = new Poly2();
b.f();
}
}
这是我的第一个 class:
public class Poly1 {
public void f() {
System.out.println( "1f" );
g();
}
private void g() {
System.out.println( "1g" );
h( 10 );
}
protected void h( int i ) {
System.out.println( "1hi" );
}
void h( byte b ) {
System.out.println( "1hb" );
}
}
下面是我的第二个 class:
public class Poly2 extends Poly1 {
protected void f() {
System.out.println( "2f" );
Poly1 c=new Poly1();
g();
h();
}
public void g() {
System.out.println( "2g" );
h( 18 );
}
public void h( int i) {
System.out.println( "2hi" );
}
public void h( byte b ) {
System.out.println( "2hb" );
}
}
public class Poly1 {
public void f() {
System.out.println("1f");
g();
}
public void g() {
System.out.println("1g");
h((byte) 10); // cast to byte to invoke the overloaded method void
// h(byte b)
}
protected void h(int i) {
System.out.println("1hi");
}
void h(byte b) {
System.out.println("1hb");
}
}
public class Poly2 extends Poly1 {
public void f() { //change from protected to public since the visibility of an overidden method Cannot be reduced
System.out.println("2f");
Poly1 c = new Poly1();
c.g(); // invoke the methode g of Poly1
h((byte) 10);
}
public void g() {
System.out.println("2g");
h(18);
}
protected void h(int i) {
System.out.println("2hi");
}
public void h(byte b) {
System.out.println("2hb");
}
}