简体   繁体   English

如何在超类中使用私有实例变量?

[英]How do I use the a private instance variable in a superclass?

I am currently learning about inheritance in my java class. 我目前正在Java类中学习有关继承的知识。 And I can't figure out why I can't use the variable "ID" using super in my subclass. 而且我无法弄清楚为什么不能在子类中使用super来使用变量“ ID”。 What is wrong with my constructor for the savings class? 我的储蓄类构造函数有什么问题? Also I am not allowed to change the instance variables in account to public. 另外,我也不允许将帐户中的实例变量更改为public。

     public class Account
{
private String ID;
private double balance;
private int deposits;
private int withdrawals;
private double service;

/**
 * Constructor for objects of class Account
 */
public Account(String IDNumber, double cashBalance)
{
    ID = IDNumber;
    balance = cashBalance;
    deposits = 0;
    withdrawals = 0;
    service = 0;
}

I get the error "ID has private access in Account" 我收到错误消息“ ID在帐户中具有私人访问权限”

    public class Savings extends Account
{
// instance variables - replace the example below with your own
private boolean active;

/**
 * Constructor for objects of class Savings
 */
public Savings(String IDNumber, double cashBalance)
{
    super(ID);
    super(balance);
    active = null;
}

super() calls the constructor for the parent class. super()调用父类的构造函数。 It has nothing to do with the private variable for that class. 它与该类的私有变量无关。

I think you ment to do: 我认为您愿意:

super(IDNumber, cashBalance);

This calls the constructor for the parent class, passing the same variables Savings was created with to the Account class. 这将调用父类的构造函数,将与创建Savings相同的变量传递给Account类。

Note that from within the class Savings you will not be able to access the parent variables. 请注意,在“ Savings ”类中,您将无法访问父变量。 You have to change the access to protected for this. 为此,您必须将访问权限更改为protected protected allows the class itself, and children access, whereas private only allows the class itself access. protected允许类本身和子级访问,而private只允许类本身访问。

暂无
暂无

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

相关问题 在更改子类中实例变量的值之后,我想在父类的方法中将超类实例变量用作参数 - i want to use superclass instance variable as arguments in the method of the superclass, after changing the values of instance variable in subclasses 什么是超类中的实例变量,以及如何在子类中更改它们的值 - What is the use instance variable in superclass , and how to change their values in subclasses 如何将子类参数传递给超类私有变量? - how do I pass subclass parameters into the superclass private variables? 通过子类实例调用时,如何在子类的方法中使用子类的静态字段? - How do I use a subclass' static fields in superclass' method when calling via instance of subclass? 我需要防止使用反射来从超类获取私有的Properties字段。 如何使用Java安全管理器执行此操作 - I need to prevent the use of reflection to get a private Properties field from a superclass. How can I use the Java security manager to do this 如何在子类中设置实例变量,即使它是私有的并在超类中声明? - How to set an instance variable in subclass even though it's private and declared in superclass? 如何在超类中与实例变量共享方法? - How do I share methods with instance variables in a superclass? 如何从超类的特定实例继承变量? - How do I inherit variables from specific instance of a superclass? 如何在Java中使用或创建私有类的变量 - How do I use or create a variable of a private class in Java 在java中,如何使用私有构造函数创建一个类,其超类也有一个私有构造函数? - In java, how do I make a class with a private constructor whose superclass also has a private constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM