当我定义一个抽象 class 而不是声明时会发生什么。
what happens when I define an abstract class,rather than declaration.
public class Temp {
public static void main(String args[]) {
A ref;
B objB = new B();
C objC = new C();
ref = objB;
ref.show();
ref = objC;
ref.show();
}
}
abstract class A {
abstract void show();
{
System.out.println("In Class A");
}
}
class B extends A {
void show() {
System.out.println("In class B");
}
}
class C extends B {
void show() {
System.out.println("In Class C");
}
}
在上面的代码中抽象方法包含了方法的定义。但是在书中指出抽象方法应该只包含该方法的声明而不是定义。当我执行这个程序时,它会产生以下输出而没有任何错误。请解释一下它是如何显示以下输出的。
In Class A
In Class A
In class B
In Class C
这个抽象方法在class中没有实现 A:
abstract void show();
分号结束没有实现的方法声明。
下面的块是一个实例初始化块(在创建class的实例时执行,在执行构造函数的代码之前),与抽象方法无关:
{
System.out.println("In Class A");
}
如果你试图给一个抽象方法一个实现,它看起来像这样并且编译失败:
abstract void show()
{
System.out.println("In Class A");
}
至于你得到的输出:
In Class A // new B() causes the instance initialziation block of A to be executed before the (default) constructor of A
In Class A // new C() causes the instance initialziation block of A to be executed before the (default) constructor of A
In class B // first ref.show() executes B's show, since you assigned objB to ref
In Class C // second ref.show() executes C's show, since you assigned objC to ref
您的摘要 class 不包含方法的定义。您的抽象 class 包含一个抽象方法和一个初始化程序块。
abstract class A {
// Abstract method
abstract void show();
// Initializer block, completely unrelated to show
{
System.out.println("In Class A");
}
}
构造器在构造对象时阻塞 运行 - 类似于构造器(但您可以有多个,并且它们不能有参数)。这和你写的完全一样:
abstract class A {
// Abstract method
abstract void show();
// Constructor
public A()
{
System.out.println("In Class A");
}
}
输出来自:
A ref;
B objB = new B(); // calls B's constructor, then A's constructor which prints "In Class A"
C objC = new C(); // calls C's constructor, then A's constructor which prints "In Class A"
ref = objB;
ref.show(); // prints "In Class B"
ref = objC;
ref.show(); // prints "In Class C"
public class Temp {
public static void main(String args[]) {
A ref;
B objB = new B();
C objC = new C();
ref = objB;
ref.show();
ref = objC;
ref.show();
}
}
abstract class A {
abstract void show();
{
System.out.println("In Class A");
}
}
class B extends A {
void show() {
System.out.println("In class B");
}
}
class C extends B {
void show() {
System.out.println("In Class C");
}
}
在上面的代码中抽象方法包含了方法的定义。但是在书中指出抽象方法应该只包含该方法的声明而不是定义。当我执行这个程序时,它会产生以下输出而没有任何错误。请解释一下它是如何显示以下输出的。
In Class A
In Class A
In class B
In Class C
这个抽象方法在class中没有实现 A:
abstract void show();
分号结束没有实现的方法声明。
下面的块是一个实例初始化块(在创建class的实例时执行,在执行构造函数的代码之前),与抽象方法无关:
{
System.out.println("In Class A");
}
如果你试图给一个抽象方法一个实现,它看起来像这样并且编译失败:
abstract void show()
{
System.out.println("In Class A");
}
至于你得到的输出:
In Class A // new B() causes the instance initialziation block of A to be executed before the (default) constructor of A
In Class A // new C() causes the instance initialziation block of A to be executed before the (default) constructor of A
In class B // first ref.show() executes B's show, since you assigned objB to ref
In Class C // second ref.show() executes C's show, since you assigned objC to ref
您的摘要 class 不包含方法的定义。您的抽象 class 包含一个抽象方法和一个初始化程序块。
abstract class A {
// Abstract method
abstract void show();
// Initializer block, completely unrelated to show
{
System.out.println("In Class A");
}
}
构造器在构造对象时阻塞 运行 - 类似于构造器(但您可以有多个,并且它们不能有参数)。这和你写的完全一样:
abstract class A {
// Abstract method
abstract void show();
// Constructor
public A()
{
System.out.println("In Class A");
}
}
输出来自:
A ref;
B objB = new B(); // calls B's constructor, then A's constructor which prints "In Class A"
C objC = new C(); // calls C's constructor, then A's constructor which prints "In Class A"
ref = objB;
ref.show(); // prints "In Class B"
ref = objC;
ref.show(); // prints "In Class C"