Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java继承类_Java_Inheritance - Fatal编程技术网

Java继承类

Java继承类,java,inheritance,Java,Inheritance,我有主课: public class MainClass{ private int firstVal; private int secVal; public MainClass(int firstVal,int secVal){ this.firstVal=firstVal; this.secVal=secVal; } } 第二个类扩展了这个类 public class SecClass extends MainClass{

我有主课:

public class MainClass{
    private int firstVal;
    private int secVal;

    public MainClass(int firstVal,int secVal){
      this.firstVal=firstVal;
      this.secVal=secVal;
    }
}
第二个类扩展了这个类

 public class SecClass extends MainClass{
     public SecClass(int firstVal,int secVal){
         super(firstVal,secVal)
     }
 }
我想知道如何在SecClass中使用值firstVal和secVal? super.firstVal?? 或者我必须用agian定义这些值


谢谢

只需将它们标记为
受保护
而不是
私有

public class MainClass {
    protected int firstVal;
    protected int secVal;
    //rest of your class...
}
如果要将这些方法保持为
私有
,可以为此字段设置getter和setter,并将这些方法标记为
公共
受保护
,具体取决于您想要/需要的可见性:

public class MainClass {
    private int firstVal;
    //its value can be retrieved by any class
    public int getFirstVal() {
        return this.firstVal;
    }
    //its value can only be modified by subclasses of MainClass
    protected void setFirstVal(int firstVal) {
        this.firstVal = firstVal;
    }
    //similar for the other field...
}

只需将它们标记为受保护的,而不是私有的:

public class MainClass {
    protected int firstVal;
    protected int secVal;
    //rest of your class...
}
如果要将这些方法保持为
私有
,可以为此字段设置getter和setter,并将这些方法标记为
公共
受保护
,具体取决于您想要/需要的可见性:

public class MainClass {
    private int firstVal;
    //its value can be retrieved by any class
    public int getFirstVal() {
        return this.firstVal;
    }
    //its value can only be modified by subclasses of MainClass
    protected void setFirstVal(int firstVal) {
        this.firstVal = firstVal;
    }
    //similar for the other field...
}

main类中需要getter和setter。然后可以调用
super.getFirstVal()

将这些添加到您的
MainClass

public int getFirstVal() {
  return firstVal;
}

public int getSecVal() {
  return secVal;
}

main类中需要getter和setter。然后可以调用
super.getFirstVal()

将这些添加到您的
MainClass

public int getFirstVal() {
  return firstVal;
}

public int getSecVal() {
  return secVal;
}

你可以用接球手和二传手。但是如果您认为子类应该能够访问它们,那么就声明
firstVal
secVal
受保护。然后您可以在子类中直接访问它们:

subclassInstance.firstVal
subclassInstance.secVal

任何人或任何子类都不允许访问私有变量。受保护的变量类似,只是子类可以访问它们。您仍然可以调用
super()
构造函数;让父构造函数初始化它们自己的变量是一种很好的做法。

您可以使用getter和setter来实现这一点。但是如果您认为子类应该能够访问它们,那么就声明
firstVal
secVal
受保护。然后您可以在子类中直接访问它们:

subclassInstance.firstVal
subclassInstance.secVal

任何人或任何子类都不允许访问私有变量。受保护的变量类似,只是子类可以访问它们。您仍然可以调用
super()
构造函数;最好让父构造函数初始化自己的变量。

如果它们在同一个包中,只需删除
private
access修饰符即可。如果没有,您需要添加
受保护的
公共
(非常糟糕的主意)。一般来说,您不应该向这样的子类公开字段,因为您不知道它们将如何处理它们。

如果它们在同一个包中,只需删除
private
access修饰符即可。如果没有,您需要添加
受保护的
公共
(非常糟糕的主意)。一般来说,您不应该向这样的子类公开字段,因为您不知道它们将如何处理它们。

下面是一个关于
public
private
protected
范围的表,从中您可以了解
private variable
不能在
子类中使用的原因,(来自)


下面是一个关于
public
private
protected
范围的表,从中您可以了解
私有变量
不能在
子类中使用的原因(从)


它们是
private
,因此您不能。您可以在子类中将其更改为protected to access。您可以在此链接中看到完整的详细信息。访问级别修改器类包子类World public Y Y protected Y Y N无修改器Y N private Y N N N N它们是
private ate
,因此您不能。您可以在子类中将其更改为protected to access。您可以在此链接中看到完整的详细信息。访问级别修改器类包子类World public Y Y protected Y Y Y N no Modifier Y N private Y Y N