【java – 构造函数必须在方法返回之前调用super()或this()】教程文章相关的互联网学习教程文章

java – 没有继承的类的构造函数中的super()【代码】

参见英文答案 > super keyword without extends to the super class 2个我在Java Video-Tutorial中看到过以下代码(类的缩写版本).public class Address {private String firstName;private String lastName;public Address() {super();}public Address(String firstName, String lastName) {this.firstName = firstName;this.lastName = lastName;}... }这里显示的类没有extend关键字.然而,在构...

java – android.app.SuperNotCalledException:Activity没有调用super.onCreate()【代码】

这是我的Android媒体播放器代码.当我在调试模式下运行MediaPlayer mp = new MediaPlayer()时使用断点运行时,我不知道我在这段代码中缺少了什么.播放zip文件夹中的所有文件.但是当我以正常模式运行应用程序时,第一个文件被播放然后我收到此错误:android.app.SuperNotCalledException: Activity {com.example.mediaplayer/com.example.mediaplayer.MainActivity} did not call through to super.onCreate()码:package com.example....

java – 为什么我的派生类方法不使用super关键字从基类调用overriden方法?【代码】

我有三个名为Human.java,Superhero.java和Run.java的类.超级英雄扩展了人类,方法introduction()在超级英雄中被重写,调用了父类的introduction().但是,当我创建一个超级英雄对象并调用介绍方法时,它不会打印基类方法的返回值.怎么了?提前致谢. Human.javapublic class Human implements Comparable<Human> {private int age;private String name;public Human(String givenName, int age) {this.name = givenName;this.age = age;}...

名单<? super B> lsb = new ArrayList(); java中的逻辑错误?【代码】

我们有:class A{} class B extends A{} class C extends B{} class D extends C{}我们可以定义列表,如:List<? super B> lsb1 = new ArrayList<Object>(); //List<? super B> lsb2 = new ArrayList<Integer>();//Integer, we expect this List<? super B> lsb3 = new ArrayList<A>(); List<? super B> lsb4 = new ArrayList<B>(); //List<? super B> lsb5 = new ArrayList<C>();//not compile //List<? super B> lsb6 = new A...

java – 构造函数必须在方法返回之前调用super()或this()【代码】

我收到此错误:Exception in thread "Thread-0" java.lang.VerifyError: Constructor must call super() or this() before return in method JGame.Util.KeyboardMap.<init>()V at offset 0at JGame.Room.Room.keyboardEventTests(Room.java:81)at JGame.Room.Room.run(Room.java:54)at java.lang.Thread.run(Thread.java:722)当我的应用程序加载时,它立即调用此方法(KeyboardMap.map是一个空的HashMap). 这是方法(第54行调用此方法...

阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第1节 继承_12-super与this关键字图解【图】

先定义一个父类。里面定义成员变量和成员方法子类继承父类,也有成员变量和成员方法,成员方法是重写了父类的。能写Override就写。这样看的就更加清楚。子类定义父类没有的方法、里面分别输出了三个num的值创建子类对象。调用show方法输出的结果子类的method里面调用父类的method方法这样子类在调用method的时候,会去把父类的执行一遍。然后再去执行子类的这既是刚才写的点的简略版内存图橙色的super_class在最后编译后的class文件...

java – 在没有构造函数的类上调用super()

我刚刚注意到在工作中分配的一个非常大的项目的代码库中,一个特定的类没有构造函数.它的子类虽然调用了super(). 有人可以解释当子类调用super()时会发生什么,但父类中没有构造函数? (我可以猜测效果就像调用一个空构造函数,但我想知道幕后是否还有其他事情发生).解决方法:如果你没有任何参数化构造函数,我严格意味着没有构造函数,那么只有java才会为你添加一个默认构造函数(一个没有参数). 每个构造函数都必须调用它的超类的构造函...

java关键字(5)super【代码】【图】

super在平时编程和面试的时候经常会被使用到,这篇文章就仔细来分析一下他的用法,并和this关键字做一个对比分析。 1、概念 它是一个指代变量,用于在子类中指代父类对象。 2、应用范围 super的三种使用情况:访问父类的方法。 调用父类构造方法。 访问父类中的隐藏成员变量。3、使用 (1)访问父类中的方法 第一步:定义father类 public class Father {private String father_a;public Father() {father_a="父亲:曹操";System.ou...

在Java中,this / super关键字是否可以表示除类/枚举之外的任何内容?【代码】

我注意到了:class A {ClassB b = new ClassB() { // anonymous class/* some expression using this */} }每当我在匿名类中使用this关键字时,this都会引用封闭的外部类/枚举,而不是匿名类. 这是否意味着这永远不能代表一个匿名类?只是“正常”的类和枚举? 此外,这个或超级代表一个接口?解决方法:您关于使用它的说法不正确.当您在匿名类中使用它时,它始终引用匿名类.除非使用OuterClassName.this,否则它永远不会引用封闭的外部类...

java – super.paintComponent(g)的问题【代码】

这是片段:protected void paintComponent(final Graphics g) {Runnable r=new Runnable() {@Overridepublic void run() {while(true) {super.paintComponent(g); // <----- line of errorg.setColor(Color.red);g.drawOval(x,y,width,height);g.fillOval(x,y,width,height);x++;y++;width++;height++;if(width==20)break;try {Thread.sleep(100);} catch(Exception exc) {System.out.println(exc);}}} };Thread moveIt=new Threa...

Java中关键字this和super的用法及注意事项

this关键字在程序中三种常见的用法: 1.通过 this 关键字可以明确地去访问一个类的成员变量,解决与局部变量名称冲突的问题; 2.通过 this 关键字调用成员方法; 3.构造方法是在实例化对象时被Java虚拟机自动调用的,在程序中不能像调用其他方法一样去调用构造方法,但是可以在一个构造方法中去使用 "this([参数1,参数2...])"的形式去调用其他构造方法。使用 this 调用类的构造方法时需要注意以下几点: ...

java – 调用super.super.method,跳过super.method【代码】

我有以下(第三方)类结构.我们将调用第三方项目ProjectSeriously,并注意我使用System.out.println代替其他复杂功能(100行代码).class A {public void hi() {// Do an important thingSystem.out.println("Important thing A");} }class B extends A { public void hi() {// Do some terrible, terrible thingsSystem.out.println("TERRIBLE THING B");// Do that important thingsuper.hi();} }现在我想写这个(这不是有效的java):c...

java – 我需要super.super.method() – >可能的设计缺陷?【代码】

我发现自己需要在java中调用super.super.method(),这是不可能的. 我只是想知道我的设计中是否存在设计缺陷? 课程:package solvers.command;/**** @author student*/ public abstract class Command {private boolean executed; //executed state/*** Constructs a new Command object.* * @modifies this.executed = false*/public Command() {this.executed = false;}/*** Executes this command.* * @modifies executed =...

使用super();在Java中【代码】

package com.example.mapdemo;import android.app.ListActivity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListAdapter; import android.wid...

java – super()如何在构造函数中工作[复制]【代码】

参见英文答案 > super() in Java 15个我有一个关于如何在构造函数中使用super()的快速问题.我知道它将如何调用超类的基础知识,但我最近查看了一些代码并且不明白它是如何在这个例子中使用的.以下是困扰我的部分的要点:public class MyClass implements MyInterface {String myString = null;public MyClass() {super();}public MyClass(String A) {super();myString = A;}public interfaceMet...

构造函数 - 相关标签