我的代码是
public class Parent
{
public Parent(int i)
{
Console.WriteLine("parent");
}
}
public class Child : Parent
{
public Child(int i)
{
Console.WriteLine("child");
}
}
我发现了一个错误:
父函数不包含接受0参数的构造函数。
我理解问题在于Parent没有带0参数的构造函数。但我的问题是,为什么我们需要一个零参数
如果父类具有带参数的构造函数,为什么在父类中需要(显式地)使用默认构造函数
class A {
A(int i){
}
}
class B extends A {
}
class Main {
public static void main(String a[]){
B b_obj = new B();
}
}
这将是一个错误。
在这个程序中,super不需要到达超类的构造函数:
class Base{
Base(){
System.out.println("Base");
}
}
public class test2 extends Base{
test2() {
//super();
System.out.print("test2");
}
public static void main(String argv[]){
test2 c = new test2();
我有以下两个课程
public class classA {
classA() {
System.out.println("A");
}
}
class classB extends classA {
classB() {
System.out.println("B");
}
}
然后运行
1
classA c = new classB();
或
2
classB c = new classB();
总是给予
A
B
为什么会发生这种情况?乍一看,在这两种场景中,我都假设只会调用classB构造
你能帮我理解一下为什么下面的代码会出现编译错误吗?如果我删除了Foo2的构造函数,它会工作得很好。
class Foo2 {
int st;
protected Foo2(int initialVal) {
this.st = initialVal;
}
}
public class Main extends Foo2 {
int st;
private Main(int initialVal) {
this.st = initialVal;
}
public static void main(
我有两节课:
import java.lang.*;
class A {
public A(int number) {
System.out.println("HI I AM INSIDE PARENT CONSTRUCTOR");
}
}
class B extends A {
public static void main(String[] args) {
A obj = new A(10);
}
}
错误:-未为默认构造函数定义隐式超级构造函数A()。必须定义显式构造函数。
为什么在创建子类构造器时需要在子类构造器中编译代码super(0)?
public class Super {
public Super(int i) {
System.out.println("super(int i)");
}
}
class Sub extends Super{
public Sub(int i) {
super(0); // why this code needed here to compile?
System.out.println("Sub(int)");
我见过一些关于这个话题的问题,但它们都是关于继承的知识。我书中的示例在继承章节之前,因此父类是java.long.Object。
1.场景: my class FotoApparat没有自定义构造函数或任何构造函数,我使用FotoApparat meinFotoApparat = new FotoApparat()创建FotoApparat实例
问题:由于我的类没有构造函数,也没有超级()调用,我假设程序检查父对象类是否有合适的构造函数,应该是新对象(),对吗?如果是,这是否仍然被认为是一个“隐式”超级()调用?
2.场景:I创建一个自定义构造函数(使用eclipse ),它接受参数。在生成的
我目前面临着一个关于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类
所以我在我正在使用的一些代码中遇到了一个小问题。从本质上讲,我有以下三段代码:
抽象类:
public abstract class TestParent {
int size;
public TestParent(int i){
size = i;
}
}
子类:
public class TestChild extends TestParent{
public void mult(){
System.out.println(this.size * 5);
}
}
实施:
public class
我有两门课以下,
abstract class Shape {
protected DrawAPI drawAPI;
protected Shape(DrawAPI drawAPI){
this.drawAPI = drawAPI;
}
public abstract void draw();
}
class Circle extends Shape {
private int x, y, radius;
public Circle(int x, int y, int radius, DrawAPI drawAPI) {
我现在正准备面试,有一个问题让我很困惑。人们说,在java构造函数中不能继承,但是这样的代码
public class childclass extends parentclass{
public childclass(){
super();
System.out.println("child");
}
public static void main(String[] args) {
childclass cc = new childclass();
}}
super()不意味着子类继承了父类的构造函数吗?
我是java的初学者,在编写代码的过程中,我遇到了一个不容易理解的问题。我的问题是“用方法编写一个类来查找矩形的面积。创建一个子类来查找矩形框的体积。”我面临的错误在下面。我写的代码也是一样的:-
class Rectangle
{
public int w;
public int h;
public Rectangle(int width, int height)
{
w=width;
h=height;
}
public void area()
{
int area=w*h;
我希望有一个带参数的构造函数,所有子类都会自动继承它,但Java不允许我这样做
class A {
public A(int x) {
// Shared code here
}
}
class B extends A {
// Implicit (int x) constructor from A
}
class C extends A {
// Implicit (int x) constructor from A
}
我不想为每个子类编写B(int x)、C(int x)等。有没有更聪明的方法来解决这个问题?
解决方案#1.创建一个可以在构造
以下代码是由Android自动生成的:
package com.example.hello.a;
import android.app.Application;
import android.test.ApplicationTestCase;
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class); // What does this line mean?
这是一个愚蠢的问题,但我已经很久没有使用java了……我如何编写布尔值的构造函数,还是应该只编写一个默认的构造函数?我最近一直在使用C++,我忘记了很多java的语法。
这就是我到目前为止所知道的:
public class Creature {
protected int terrain;
public static final int DESERT = 0;
public static final int MOUNTAIN = 1;
public static final int FOREST = 2;
//symbols on cards
编辑2:顺便说一句,我在类中的代码工作正常,并且通过了所有的测试人员。我只是在试着理解构造函数。Date d是一个对象;是Date的构造函数的乘积,对吗?因此,如果IncDate是一个Date (我认为这就是扩展的意思),那么它是否可以访问Date的构造函数,从而能够创建新的Date对象和/或使用它们?再次,各位,非常感谢!
例如:在我的数据结构类中,我正在处理的类看起来像这样。这是子类的构造函数:
public class IncDate extends Date
public IncDate(int newMonth, int newDay, int newYear)
{
supe
我有这样一门课,Person:
public class Person{
String firstname;
String lastname;
public Person(String fname, String lname){
}
public String toString(){
}
}
还有这个子类,学生:
public class Student extends Person{
Student(){
super();
}
int studentID;
int level;
public Student(String fName
你们能不能给我解释一下为什么这个例子的输出是:
Exam1
This is Tennis Shoes
derived class
This is a white Tennis Shoes is created
如果你能向我解释一下,在我们创建了一个White Tennis Shoes对象之后到底发生了什么,以及发生了什么。请不要告诉我做你们的家庭作业或者我正在准备我的考试,这是一个古老的问题。
代码如下:
class Shoes{
public Shoes(){
this("you have created a shoes Object"
我在这个子类上有点困难,这是一个项目的子类。我的构造函数需要将minPassingAvg变量回调到基类(子类),但不断收到编译器错误(无法转换为字符串),这甚至不是我的本意,并且用作我在下面使用的布尔值的条件,该布尔值总是返回false。在这一点上,我不确定在我的当前代码中需要或不必要的是什么。 import java.util.List;
public class MonitoredKid extends Kid{
public int minPassingAvg;
public MonitoredKid(int minPassingAvg)
为什么代码不能编译?那么,它不能编译的原因是什么?有人能为我陈述或阐述它吗? class Pet{
public Pet(int age){
System.out.print("Pet age: " + age);
}
}
public class Cat extends Pet{
public Cat(){
System.out.print("Cat");
}
public static void main(String[] args) {
new Pet(5);
我在寻找考试准备问题时偶然发现了这段代码。我不明白在这段代码中调用超类构造函数是什么?
输出量为->猫美洲狮cc。
THL
public class Feline{
public String type = "f ";
public Feline(){
System.out.print("feline ");
}
}
-
public class Cougar extends Feline {
public Cougar(){
这是我的超级班:
public abstract class BankAccount
{
private String name;
private double balance;
final int OVERDRAFT_FEE = 35;
private static int accountNum=0;
public BankAccount(String name)
{
this.name=name;
balance=0;
accountNum++;
}
//various met
我为2d游戏创建了一个抽象的shape类,但是在两个shape类中我都得到了一个错误。这个错误与super()有关。也许还有其他错误。我还展示了代码中的错误所在。super()适合使用。
Shape类
public abstract class Shape {
int Y;
int WIDTH;
int HEIGHT;
int DIAMETER;
public Shape(int Y, int WIDTH, int HEIGHT, int DIAMETER) {
this.Y = Y;
this.WIDTH = WID
我就是这样遇到这个问题的。我举一个例子:
package check;
public class Check {
int a,b;
Check (int i,int j) {
a = i;
b = j;
}
}
这很好。现在,我想通过扩展Check来创建一个子类。所以我写到:
class V extends Check {
}
我一写完它,Eclipse中就出现了一个十字架,点击它,我找到了一条消息:
implicit super constructor Check() is undefined for default constructor. Must def
在Java中必须调用基类构造函数吗?在C++中,这是可选的,所以我提出这个问题。
当我扩展ArrayAdapter时,我得到这个错误:"Implicit super constructor ArrayAdapter<String>() is undefined. Must explicitly invoke another constructor"
那么,调用基构造器的目的是什么呢?当我创建对象基类时,构造函数会调用&然后它就会派生正确。