我有两个类Parent和display子类,我调用parent overridden ( Child.From ).From父类,我调用另一个方法(Display),但由于调用了哪个子方法,该方法也被覆盖。我想从show方法调用父方法display。
public class Parent {
public void show()
{
System.out.println("Show of parent ");
this.display();
}
public void display()
{
public class Demo extends xyz implements abc {
public static void main(String[] args) {
abc a = new Demo();
xyz x = new Demo();
Demo w = new Demo();
a.show();
x.show();
w.show();
}
}
interface abc {
void show();
}
class xyz {
pu
在执行基本方法重载程序时,我经常会收到以下错误:
Sh-4.3美元javac HelloWorld.java
HelloWorld.java:10: error:方法显示()已经在类HelloWorld中定义
静态空洞显示()
HelloWorld.java:25: error:方法显示(int,int)已经在类HelloWorld中定义
静态空隙显示( int a,int b)
2个错误
该程序的代码如下
public class HelloWorld{
static int show()
{
int c = 5+10
我有一个普通案件的代码:
public class A {
public String show(A obj) {
return ("A and A");
}
}
public class B extends A {
public String show(B obj) {
return ("B and B");
}
public String show(A obj) {
return ("B and A");
}
}
public class C
在以下代码中:
b.show("str");
//prints: from base
d.show("");
//prints: from str
有人能解释一下为什么它的行为不同吗?我想知道为什么要调用基类中的b.show() Base = new ()。我只是使用DifferentClass作为引用,表明b.show(String)是在非继承情况下调用的。
public class TestClass {
public static void main(String[] args) {
Base b = new Sub();
关于接口实现和扩展类的小问题。这是我的代码。
public class intercheck extends inter implements in {
public static void main(String[] args) {
intercheck a = new intercheck();
a.show();
}
}
class inter {
public void show() {
System.out.println("Class Show");
}
}
interface in {
public void show
我有一个涉及继承、多态性、重载和重写的问题。我理解所有这些术语,但不确定它们在这里是如何工作的。代码如下:
class A {
public String show(D obj) {return "A and D";}
public String show(A obj) {return "A and A";}
}
class B extends A {
public String show(B obj) {return "B and B";}
public String show(A obj) {return
我正在做一个使用lua中间类库的项目。我的实验表明,如果一个类包含一个与类函数同名的函数,那么在类的一个实例上调用该函数将调用类函数,而不是 mixin 函数。
但我不明白为什么要读中级代码。行为是有保证的,还是依赖于某个呼叫或包括订单?
编辑:在下面添加了最小的代码。我可以依赖myFoo:show()显示'Foo‘吗?
class = require('middleclass')
-- this is a middleclass mixin, not a class
Bar = {}
function Bar:show()
print("Bar
我对Java重写有一点困惑。假设我们有以下继承:
class A{
public A(){
}
void show(){
System.out.println("SuperClass");
}
}
class B extends A{
@Override
void show(){
System.out.println("SubClass");
}
}
public class Test {
public static void main(Strin
在尝试不同的排列和方法重写组合时,我遇到了一个奇怪的无限循环。当从超类中的mutator方法调用超类的重写方法时,程序进入无限循环。我使用的代码如下:
class A {
private int x = 10;
public void show() {
System.out.println("Inside A's show showing private X: " + x);
}
public void change(int num) {
this.x = num;
//
我对Haskell非常陌生,但我想知道如何编写代码更短的代码:
data Suite = Club | Heart | Spade | Diamond
data Value = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen |
King | Ace
data Card = Card Suite Value
instance Show Suite where
show Club = "Club"
show Heart = "Heart"
给出了带有prr类的public abstract prr()。根据官方的java文档,不应该将抽象和私有关键字放在一起,但是它在Eclipse中编译
abstract class prr {
/* we have an abstract class as prr. There is a method as show.*/
abstract private void show();
}
// Since show() is abstract, this is redefined in App class.
// Ideally show() should not even be seen in
在下面的示例中,最后一行B().show()没有调用适当版本的show函数。它应该调用子版本,而不是父版本。我想我应该做一些像__class_method()这样的事情,但是找不到一个完整的答案。
我当然可以在B中重写show函数,但这本质上意味着复制并粘贴show函数。这并不优雅。
## version one ##
class A(object):
def method(self):
print("hello")
def show(self):
self.method()
class B(A):
def met
我是java新手,现在我有一个关于方法覆盖的问题:
public class maintest {
public static void main(String[] args) {
new Zi();
}
}
public class Fu {
public Fu() {
show();
}
public void show() {
System.out.println("This is from Fu show");
}
}
public class Zi extends F
public class Demo1{
public static void main(String[] args){
show('A','A');
}
public static void show(char c, long a){
System.out.println("long-char");
}
public static void show(char c, int a){
System.out.println("cha
我只是试着理解下面这段代码,但在最后无法理解这两行代码。
ptr = &D;
ptr->show();
我实际上是在学习多态性概念的路上。这只是最基本的。因此,请解释一下它是如何工作的,以及为什么ptr->show()在base中调用函数。
#include <iostream>
using namespace std;
class Base {
public:
void show()
{
cout << "This is the base\n";
我在学红宝石。为了学习目的,我编写了一个嵌套模块的代码,如下所示。我的任务是从C模块打印输出"IN FIRST A“。我尝试了Object::A::A.show和::A::A.show,但是它是”在第二个A中“打印输出。我也尝试过A::A.show,但它将错误作为未初始化的常量。请帮助理解它为什么要打印“在第二A”,以及我需要对打印机“在第一A”输出做什么
module A
def self.show
puts "in outer A"
end
module A
def self.show
puts "IN FIR
class A{
int a=10;
public void show(){
System.out.println("Show A: "+a);
}
}
class B extends A{
public int b=20;
public void show(){
System.out.println("Show B: "+b);
}
}
public class DynamicMethodDispatch {
public static void main(String[] args) {
由于Groovy动态方法调度,下面的代码打印ABC,这正是我所期望的。在添加@CompileStatic之后,我的代码中断,只打印AAA。如何保留动态方法选择的逻辑并静态编译代码?
class PatternMatching {
static void main(String[] args) {
[new A(), new B(), new C()].each {
show it
}
}
static void show(A a) {
print 'A'
}
s
当我编写a=c时,我只是想了解发生了什么;当我检查"a“类型时,它显示为"C”类。现在我的问题是,如果"a“指向"c”,那么为什么它的行为不像指针"c“。
class Program
{
static void Main(string[] args)
{
C c = new C();
A a = new A();
Console.WriteLine(a.GetType());
a = c;
Co
我使用两个具有相同方法签名的接口,并尝试在名为Parent和child的类中实现。然而,我搞不懂为什么协变返回类型在这里不起作用。
interface First
{
public long show();
}
interface Second
{
public int show();
}
abstract class Parent implements First,Second
{
public long show(){
re
我有一个代码:
interface My {
//this is left empty
}
class Test implements My {
public static void main(String[] args){
My m=new Test();
String str=m.toString(); //how reference variable m calls toString() in Test even it is not in My?
System.out.println(str);
st
在抽象类测试中,当我声明‘抽象公共空显示();’时,是创建了一个全新的show()方法,还是仅仅引用了接口Inter中声明的show()方法?请澄清。
interface Inter
{
void show();
}
abstract class Test implements Inter
{
abstract public void show(); //What does this line signify?
}
在基类中不覆盖public final或protected final方法很简单,当我们在父类中创建方法private final时,即使您将父类设置为private final,它也会被覆盖,并在子类中使用它覆盖的方法protected final扩展它。有人能解释这种行为吗?
class A {
private final void show() {
System.out.println("Show method from A");
}
}
class B extends A {
protected final void sho
bb引用派生类,当我调用show()方法时,但是当我打印bb.b -是print基类属性时。为什么会发生这种情况?
class Base {
int b=10;
public void show() {
System.out.println("Base show() called");
}
}
class Derived extends Base {
int b=20;
public void show() {
System.out.println("Derived show() ca
正常覆盖(不使用接口)
class A {
int x = 0;
void show() {
System.out.println("In A. x : " + x);
}
}
class B extends A {
void show() {
System.out.println("In B. x : " + x);
}
}
class C extends A {
void show() {
System.out.println("In C. x : &
我对java中的动态绑定有点困惑。这是一个程序,我想知道,是发生了动态绑定还是别的什么。什么是动态绑定?
class A {
int a;
public A() {
a = 9;
}
public void show() {
System.out.print("show in A ; ");
System.out.println("a : " + a);
}
}
public class B extends A {
public B() {
a = 8;
}
public void show() {
System.o
据说Java不支持多重继承,但可以实现多个接口。因此,对于下面的代码片段,将实现show()方法中的哪一个。
public interface I1{
public void show();
}
public interface I2{
public void show();
}
class mine implements I1, I2{
@override
public void show(){
//do something
}
}
我的问题是,我如何知道被覆盖的show()方法。
类B中的show()方法是“重写的”、“实现的”还是简单的“定义的”?
interface A
{
void show();
}
class B implements A
{
public void show()
{
System.out.println("What is the proper term to explain?");
}
}
有4个类: class Lower extends Middle {
private int i;
private String name;
public Lower(int i){
super(i+1);
name = "Lower";
this.i = i;
}
public void set(Lower n){ i = n.show();}
public int show(){return i;}
}
class Middle extends Upper