在覆盖活动的ondestroy时,我应该在super.onDestroy()之前或之后放置命令吗?
protected void onDestroy() {
//option 1: callback before or ...
super.onDestroy();
//option 2: callback after super.onDestroy();
}
Run Code Online (Sandbox Code Playgroud)
(现在我担心:如果super.onDestroy太快,它将永远不会到达选项2.)
当构造函数没有显式调用超类构造函数(或this())时,编译器就会插入super().
如果从类文件中删除此调用(编译后)会发生什么?
为什么子类构造函数必须显式调用超类构造函数?这是什么原因?
在这段代码中,我理解它适用于A类中的super(t)或B类中的no args构造函数.下面的代码使用了B类中的no args构造函数.我理解如果你将no args构造函数取出来B类的代码不起作用.我是编程的新手,我想要了解的是B类中没有args构造函数的特殊之处,为什么它必须存在才能使代码工作?特殊条件或规则是什么?
public class Test {
public static void main(String[] args) {
B b = new B(5);
}
}
class A extends B {
public A(int t) {
System.out.println("A's constructor is invoked");
}
}
class B {
public B() {
}
public B(int k) {
System.out.println("B's constructor is invoked");
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个构造函数,它获取HashSet和HashMap.我需要在一个hashMAp上运行验证检查并将其与hashSet结合使用,因为'super'必须只接收一个hashSet.因为我得到以下错误,我无法找到方法:cannot reference this before supertype constructor
例:
public class A extends B {
public A(HashSet<Obj> h1, HashMap<UID,Objects> m1) {
super(new C (h1) ); //h1 should contain changes related to m1..
}
Run Code Online (Sandbox Code Playgroud)
我想做那样的事情:
public class A extends B {
public A(HashSet<Obj> h1, HashMap<UID,Objects> m1) {
runMyFunc(h1,m1);
super(new C (h1) );
}
runMyFunc(HashSet<Obj> h1, HashMap<UID,Objects> m1){
//do checks
//more checks...
// if something then h1.setUid(m1.get(0))...
return h1;
}
Run Code Online (Sandbox Code Playgroud)
我想将构造函数转换为私有,然后像这样运行它:
public class A extends B {
private A(HashSet<Obj> h1) {
super(new C …Run Code Online (Sandbox Code Playgroud) 我正在学习有关如何使用this()重载的构造函数,并遇到了以下限制:
您不能在调用this()时使用构造函数类的任何实例变量。
例如:
class Test{
int x;
public Test() {
this(x); //Does not compile
}
public Test(int y) {}
void method1() {
method2(x); //OK
}
void method2(int y) {}
}
Run Code Online (Sandbox Code Playgroud)
我知道不需要将实例字段传递给构造函数,因为默认情况下它是可见的。但是,为什么相同的限制不适用于实例方法?
我正在尝试理解Java super()构造函数.我们来看看下面的课程:
class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point() {
this(0, 0);
}
}
Run Code Online (Sandbox Code Playgroud)
这个类将编译.如果我们创建一个新Point对象Point a = new Point();,那么将调用没有参数的构造函数:Point().
如果我错了,请纠正我,在做之前this(0,0),Class将调用构造函数,然后Point(0,0)才会调用它.如果这是真的,那么说super()默认情况下是否正确?
现在让我们看一下相同的代码并进行一些小改动:
class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point() {
super(); // this is the change. …Run Code Online (Sandbox Code Playgroud) 正如我们在子类的构造函数体中所知,父构造函数必须是第一个语句,否则我们会得到编译时错误.这里已经讨论过这个主题.
让我们假设调用父构造函数会导致系统资源成本过高.另一方面,在子类构造函数中我们需要首先检查一些条件,如果条件满足,我们很好地通过父构造函数,那么就没有必要更进一步(假设抛出异常):
class parent {
parent(Object blah) {
//Heavy resource consuming tasks
}
}
class child extends parent {
child(Object blah, boolean condition) {
if (!condition) throw new IllegalArgumentException("Condition not satisfied");
super(blah); //Compile error!
}
}
Run Code Online (Sandbox Code Playgroud)
如果有人有同样的问题,我很好奇无论如何要处理这种情况,或者我必须先调用父构造函数,无论浪费多少资源然后抛出异常?
我刚在学校学到了,但老师不知道为什么.
我可以想出一些很好的理由,但是我认为有些情况下可以在构造函数中稍后进行初始化 - 例如,在使用母类的变量之前.好的,变量应该从一开始就被初始化,但这并不总是必要的.
我猜是有更多的理由说明为什么必须将super()放在构造函数的第一行.
那么,为什么我必须在构造函数的第一行编写super(),当我继承一个类时?
我有两节课.超类:
public abstract class Question(){
public Question(String question, String answer, String... alts){...
}
Run Code Online (Sandbox Code Playgroud)
而子类:
public class StringOptionsQuestion extends Question {
public StringOptionsQuestion(String question, String answer, String... alts){
if (alts.length == 0){throw new IllegalArgumentException();} //The compiler doesn't like this line.
super(question, answer, alts);
}}
Run Code Online (Sandbox Code Playgroud)
我希望我的子类,在它传递给超类的构造函数之前StringOptionsQuestion验证其长度alts.但是,我不能这样做.我得到错误"构造函数调用必须是构造函数中的第一个语句".为什么会出现此错误?我怎么能绕过它呢?
为什么不编译?
class test
{
constructor() {
var a = Date().day
this(a)
}
constructor(a:Int) {
}
}
Run Code Online (Sandbox Code Playgroud)
错误是:无法将类型为“ test”的表达式“ this”作为函数调用。找不到函数“ invoke()”。
建议的解决方案是添加以下内容:
private operator fun invoke(i: Int) {}
Run Code Online (Sandbox Code Playgroud)
为什么?
我有一个类在 Java 中扩展了另一个类
我需要构造函数来运行超级构造函数
这是我的基本代码:
public class Polygon implements Geometry {
public Polygon(Point3D... vertices) {
......
}
}
Run Code Online (Sandbox Code Playgroud)
我想从子类调用它
public class Triangle extends Polygon {
//ctor
public Triangle(Point3D point3d, Point3D point3d2, Point3D point3d3){
List<Point3D> _temp = null;
_temp.add(point3d);
_temp.add(point3d2);
_temp.add(point3d3);
super(_temp);
}
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做,因为我收到错误“构造函数调用必须是构造函数中的第一条语句”,但我需要构建构造函数
谢谢
java ×10
constructor ×4
inheritance ×3
super ×2
.class-file ×1
abstract ×1
android ×1
class ×1
destroy ×1
kotlin ×1
superclass ×1
validation ×1