gpt4 book ai didi

java - 这是抽象类的实例化吗?

转载 作者:搜寻专家 更新时间:2023-11-01 04:06:38 26 4
gpt4 key购买 nike

If Abstract class cannot be instantiated, then how can the variables access and methods access of the abstract class A even without extending is achieved in the class check (as you can see in the below code)

Is the created a an object of abstract class A?

代码

    abstract class A
{
int a=10;
public A()
{
System.out.println("CONSTRUCTOR ONE");
}
public A(String value)
{
System.out.println("CONSTRUCTOR "+value);
}
void add(int sum)
{
System.out.println("THE SUM IS:"+sum);
}
int sub(int a,int b )
{
return(a-b);
}
}

public class check
{
public check()
{
new A("TWO"){};
}
public static void main(String args[])
{
int a,b,sum;
a=10;
b=15;
sum=a+b;
A s = new A() {};
new check();
s.add(sum);
int subb=s.sub(35,55);
System.out.println("THE SUB IS:"+subb);
System.out.println("THE VALUE OF A IS:"+s.a);
}
}

输出

CONSTRUCTOR ONE
CONSTRUCTOR TWO
THE SUM IS:25
THE SUB IS:-20
THE VALUE OF A IS:10
BUILD SUCCESSFUL (total time: 0 seconds)

最佳答案

new A() {} 调用创建了一个匿名子类并将其实例化。因为 A 不包含任何抽象方法,所以这是可行的。

关于java - 这是抽象类的实例化吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8132091/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com