简体   繁体   English

我可以使用Java公开受保护的成员吗? 我想从子类访问它

[英]Can I make a protected member public in Java? I want to access it from a subclass

I'm new to Java and OOP, 我是Java和OOP的新手,

I was using a private subclass (actually a struct) B in a class A, and everything went well until I decided to make a parent class C for subclass B. I want make public some of the protected members of class C. 我在A类中使用了私有的子类B(实际上是结构)B,一切顺利,直到我决定为B类创建父类C。我想公开一些C类的受保护成员。

For example: 例如:

public class A {
   private class B extends C {
       public int product;
       public int x;
       public int y;
       public void add() {
             product=x+y;
       }
   }
   B b=new B;
   b.x=1;
   b.y=2;
   b.multiply();
   System.out.println(b.product+"="+b.x+"x"+b.y);

public class C {
   protected int x;
   protected int y;
   public int sum;
   public C(px,py) {
       x=px;
       y=py;
   }
   public void sum() {
       sum=x+y;
   }
}

And I get 我得到

Implicit super constructor C() is undefined for default constructor. 对于默认构造函数,未定义隐式超级构造函数C()。 Must define an explicit constructor 必须定义一个显式构造函数

Of course, I could remove extends C, and go back to what I had before. 当然,我可以删除扩展C,然后回到以前的状态。 Or I could make a getter/setter. 或者我可以做一个吸气剂/设置者。 But I think it is understandable that an inner struct is acceptable, and it should be able to extend other classes. 但是我认为内部结构是可以接受的,并且应该能够扩展其他类是可以理解的。

The compiler message is reasonably clear - in B you've effectively got: 编译器消息相当清晰-在B中,您实际上得到了:

public B() {
    super();
}

and that fails because there's no parameterless constructor in C to call. 失败了,因为C中没有要调用的无参数构造函数。 Either introduce a parameterless constructor, or provide an explicit constructor in B which calls the constructor in C with appropriate arguments. 要么引入一个无参数的构造函数,要么在B中提供一个显式构造函数,该构造函数使用适当的参数调用C中的构造函数。

I'm not sure it's a good idea to have all these non-private fields, mind you - nor is it a good idea for fields in B to hide fields in C. Do you really want an instance of B to have two x fields and two y fields? 请注意,我不确定是否拥有所有这些非私有字段是一个好主意-B中的字段隐藏C中的字段也不是一个好主意。您是否真的希望B的实例具有两个x字段和两个y场? You realise they will be separate fields, don't you? 您意识到它们将是单独的字段,不是吗?

If you just want to effectively provide public access, you could have: 如果您只想有效提供公共访问权限,则可以:

public void setX(int x) {
    this.x = x;
}

public int getX() {
    return x;
}

(and the same for y ) and remove the extra fields from B. You can't change the actual accessibility of the fields in C though. (与y相同)并从B中删除多余的字段。不过,您无法更改C中字段的实际可访问性。

好的,我在弄乱自己的代码,发现问题是我需要超类C的受保护的默认构造函数。它现在可以工作...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您可以使超类的公共成员成为子类中的受保护成员吗? - can you make a public member of the superclass a protected member in a subclass? 如何从ColdFusion访问Java类的公共静态成员? - How can I access a public static member of a Java class from ColdFusion? Java 从子类访问受保护的变量 - Java access protected variable from subclass Java:仅打算覆盖的库方法的受保护或公共访问? - Java: protected or public access for library methods that I intend to override only? Java:无法访问扩展子类中超类的受保护成员 - Java: cannot access a protected member of the superclass in the extending subclass 如何在子类中公开受保护的静态字段? - How can I make a protected static field public in sub class? 我怎样才能只获得java类的受保护的公共构造函数? - How can I get only protected and public constructors of a java class? 是否可以从 Java 中的“相同包非子类”访问受保护的成员? - Is protected member accessible from “same package non-subclass” in Java? 我可以从抽象超类上的方法访问子类上定义的静态成员变量吗? - Can I access static member variables defined on the subclass from a method on the abstract superclass? 如何从子类访问私有成员 - How do I access a private member from a subclass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM