编译完程序后,我收到了显示给我的错误信息
Resource: Employee.java
int cannot be dereferenced
Run Code Online (Sandbox Code Playgroud)
这个代码
public Salary(int positionSalary) {
setMonthlySalary(positionSalary);
}
Run Code Online (Sandbox Code Playgroud)
经过一段时间的检查后,我仍然无法修复错误.这是我的班级,
雇员
import java.util.*;
import java.lang.String.*;
public class Employee {
protected int type;
private static int staticID = 0001;
protected int id;
protected String name;
protected String ic;
protected String tel;
protected static int count = 0;
protected int editChoice=0;
private Salary salary = new Salary(0);
public Employee(){
id = -1;
name = "";
ic = "";
tel = "";
}
public Employee(int type, String name, …Run Code Online (Sandbox Code Playgroud) 所以我有两个班:Property和Houses.Property是抽象的超类,Houses是它的子类.
这是代码 Property
public abstract class Property{
String pCode;
double value;
int year;
public Property(String pCode, double value , int year){
this.pCode = pCode;
this.value = value;
this.year = year;
}
public Property(){
pCode = "";
value = 0;
year = 0;
}
public abstract void depreciation();
//Accessors
private String getCode(){
return pCode;
}
private double getValue(){
return value;
}
private int getYear(){
return year;
}
//Mutators
private void setCode(String newCode){
this.pCode …Run Code Online (Sandbox Code Playgroud) 为什么即使你已经声明了一个私有的静态最终变量,一个颜色 - 默认C,让我们说 - 然后你仍然不能this.defaultC在超级构造函数中使用(即你只能使用super(defaultC)而不是super(this.defaultC),即使它只是简单的等价defaultC.
我只是试图将Tile类扩展为Wall类,而Wall类我存储了所有墙的所有必要private static final变量(例如它们的宽度和高度,另一个int和默认颜色),而Tile类已经有了一些变量(但它们protected不是private).我不希望Field hides another field弹出警告(因为Tile.java和Wall.java对于许多变量都有相同的名称),所以我使用this.了我的私有静态最终变量,并且有很多错误.
这并不是特别令人不安(因为我只是有一些警告),但我只是想知道为什么.我猜测编译器根本不喜欢它,因为你不能this在超类型之前引用,但仍然完全相同.是否还没有添加这样的功能,忽略了这样的事情,还是有其他原因我看不到你不能使用super(this.PRIVATE_STATIC_FINAL_VARIABLE);?
我学习java继承,这是我的父类和子类
public class ParentClass {
String name;
final String subject = "Java";
public ParentClass(String name) {
this.name = name;
}
void intro () {
System.out.println("Learn " + this.subject);
}
}
class ChildClass extends ParentClass {
ChildClass(String name) {
super(name);
}
void getMethodParent() {
intro();
System.out.println(subject);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么我仍然可以在不使用 super 关键字的情况下访问父类的方法或变量?就像getMethodParent中的方法intro一样,它应该是super.intro()。
我用:
像这样:
public class class1 {
public void xxx(){
super.getXXX();
super().getXXX();
}
}
Run Code Online (Sandbox Code Playgroud) 您好我试图从一个扩展其超类及其构造函数的类实例化一个对象,但是在实例化对象的构造函数中接受Java接受参数很难,有人可以帮帮我,谢谢!这是程序:
public class Box {
double width;
double height;
double depth;
Box(Box ob)
{
this.width=ob.width;
this.height=ob.height;
this.depth=ob.depth;
}
Box(double width, double height, double depth)
{
this.width=width;
this.height=height;
this.depth=depth;
}
double volume()
{
return width * height * depth;
}
}
public class BoxWeight extends Box{
double weight;
BoxWeight(BoxWeight object)
{
super(object);
this.weight=object.weight;
}
BoxWeight(double w, double h, double d, double wei)
{
super(w,h,d);
this.weight=wei;
}
}
public class Proba {
public static void main(String[] args) {
BoxWeight myBox1 = new …Run Code Online (Sandbox Code Playgroud) 我们知道可以从子类的构造函数中调用父类的构造函数.它可以通过调用super来完成,super应该是构造函数的第一个语句.
它是否只是为了增加OOP概念的特征数量而保留的功能还是有任何实际应用?
如果有任何实际应用,请说明.
我要求实际应用.这意味着一个现实生活场景,使用此功能非常有用,而不是以其他方式完成工作.
每个人都只是发布概念或代码如何工作.但我已经知道了.我只想要一个实际的场景,它可以让我免于编写大量代码或者无法做其他代码.
我如何创建如下的父子继承....
class Synset(object):
def __init__(self, synset_id=None, w_num=None, word=None, ss_type=None, sense_number=None, tag_count=None):
self.synset_id = synset_id
self.w_num = w_num
self.word = word
self.ss_type = ss_type
self.sense_number = sense_number
self.tag_count = tag_count
def __repr__(self):
return repr((self.word))
class NounSynset(Synset):
def __init__(self, synset_id=None, w_num=None, word=None, ss_type=None, sense_number=None, tag_count=None):
super(NounSynset, self).__init__(synset_id=None, w_num=None, word=None, ss_type=None, sense_number=None, tag_count=None)
pass
ns = NounSynset(None, None, "Word")
print ns
Run Code Online (Sandbox Code Playgroud) 我如何知道 super() 在 MRO 中到底采用哪条路径?
super ×9
java ×7
inheritance ×3
python ×2
abstract ×1
constructor ×1
final ×1
jcreator ×1
oop ×1
python-3.5 ×1
python-3.x ×1
static ×1
subclass ×1
supertype ×1