我目前面临着一个关于super关键字的理解问题。我知道super是对特定子类的下一个超类的引用。不管怎样,我想告诉你我的问题:
public class Father{
int length;
Father(String s){
length = s.length();
}
}
public class Sub extends Father {
char c;
Sub(String s) {
super(s);
c = s.charAt(0);
}
}
代码行super(s)在Sub类
在Python 2中,有两种方法可以调用从父类继承的方法。在super中,方法显然来自父类,而不包含super。
class Parent(object):
def greet(self):
print('Hello from Parent')
class Child(Parent):
def __init__(self):
super(Child, self).__init__()
def hello(self):
print('Hello from Child')
s
super关键字可用于访问父类的成员。除此之外,super也是子类的一个实例,下面的代码证明了这一点:
class A {
}
class B extends A {
public void method() {
System.out.println(this.getClass().isInstance(new B()));
System.out.println(super.getClass().isInstance(new B()));
}
}
public class InheritanceDemo {
public stati
下面我遇到了JavaScript(ES6)的一个问题
class A{
constructor(){
this.foo();
}
foo(){
console.log("foo in A is called");
}
}
class B extends A{
constructor(){
super();
this.foo();
}
foo(){
console.log("foo in B is called");
}
}
我期望的是
foo in A is called
foo in B
我知道Super是一个引用变量,用于引用直接父类对象。但是我发现super和this的哈希代码是相同的,这意味着它们引用子对象。
那么,如何使用super来调用父类对象呢?
样本代码:
class Animal{
int a;
Animal(){
System.out.println("animal constructor ");
}
}
class Dog extends Animal{
int b;
Dog(){
System.out.println("dog constructyor
我试图为这句话找到证据--关键字super是对父类的引用,就像关键字this是对当前类的引用一样。
我在Java >B->C中尝试多级继承:A类是父类,B类是父类,C类是子类。
我在所有三个类中分别声明了一个变量X (A:x=100、B:x=200、C:x=300)。
在子类构造函数中,我打印值。但是,强制转换不适用于super关键字,而适用于this关键字。
((A)super).x不工作,但((A)this).x工作。
class A {
int x = 100;
}
class B extends A {
int x = 200;
}
public cla
我是objective c的新手。我知道-init()是一个instance method并返回一个对象,例如myObj=[myObj init];将返回一个对象myObj.
但是,如果self =[super init];通常是super refer to parent类,例如NSObject,它是一个class, not instance。
所以,Is -init() instance method or class method for super init?
谢谢
我想我知道super打算覆盖从父类继承的相同方法。但是,在下面的消息传递代码示例中,我不知道super应该在那里做什么:
class WellBehavedFooCatcher
def respond_to?(method_name)
if method_name.to_s[0,3] == "foo"
true
else
super(method_name)
end
end
end
那么,super在上面的代码中做了什么呢?
提前谢谢你!
假设我有以下课程:
public class Foo {
public Foo()
{}
public String who() {
return "I'm foo ";
}
public String info() {
return who() + "and i provide foo infos";
}
public static void main(String[] args)
{
Foo bar = new Bar()
来自Android的教程:
static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurren
public class Counter{
private int value;
public int getValue(){
return value;
}
public void setValue (int value){
this.value = value;
}
public void increment() {
/* */
}
public void decrement() {
/* */
}
}
public class Lo
JVM调用超类构造函数的必要性是什么?
public class Test1{
public void dis()
{
System.out.println("In Test1");
}
}
public class Test2 extends Test1{
public void dis()
{
System.out.println("Inside test2");
super.dis();
}
}
既然dis()是一个实例方法,既然构造函数是自动调用的,那么假设