Tag: inheritance

实现不兼容的接口

我正在尝试构建一个实现Queue和Map 。 两个接口都定义了remove(Object)方法,但具有不同的返回类型: public interface Collection { //Queue extends Collection, which has the problem method public boolean remove(Object e); //… } public interface Map { public V remove(K key); //… } public class QueuedMap extends AbstractMap implements Queue { public V remove(K key) {/* … */} //ERROR: V is not compatible with boolean //… } […]

使用子类对象访问超类函数

我有一个子类的对象扩展其超类。 子类中有一个重写方法,可以使用该对象调用。 是否可以使用子类对象调用超类的函数? package supercall; public class Main { public static void main(String[] args) { SomeClass obj = new SubClass(); obj.go(); //is there anything like, obj.super.go()? } } class SomeClass { SomeClass() { } public void go() { System.out.println(“Someclass go”); } } class SubClass extends SomeClass { SubClass() { } @Override public void go() { […]

如何使toString()方法返回Super Class私有字段及其实例字段?

有没有办法让toString()包含超class私有字段? 我尝试添加super.toString() ,但没有用。 请参阅下面的代码 Employee.java package test; public class Employee { private String name; private int id; private double salary; public Employee(String name, int id, double salary) { super(); this.name = name; this.id = id; this.salary = salary; } public double getSalary() { return salary; } @Override public String toString() { return “Employee [name=” + […]

获取Java类的行为

我在java中有一个generics类定义为: public static class KeyCountMap { private Map map = new LinkedHashMap(); // … rest of the properties… public KeyCountMap() { } @SuppressWarnings({ “unchecked”, “rawtypes” }) public KeyCountMap(Class mapType) throws InstantiationException, IllegalAccessException { map = mapType.newInstance(); } //… rest of the methods… } 我在.NET中定义了相同的类: public static class KeyCountMap { private Dictionary map = new Dictionary(); […]

Javainheritance与C#inheritance

假设Java有这些分层类: class A { } class B extends A { public void m() { System.out.println(“B\n”); } } class C extends B { public void m() { System.out.println(“C\n”); } } class D extends C { public static void main(String[] args) { A a = new D(); // am(); // doesn’t work B b = new D(); […]

jUnit忽略基类中的@Test方法

假设我有一个名为testFixtureA的测试类, testFixtureA有几个方法testA , testB , testC等,每个方法都有@Test注释。 现在让我说我将testFixtureA子类化为名为testFixtureAB类,我不会覆盖任何东西。 testFixtureAB现在是空的。 当我从testFixtureAB运行测试时, testA , testB和testC方法由测试testC器执行,因为测试运行器不区分类和基类的测试方法。 如何强制测试运行器从基类中省略测试?

java构造函数错误:类中的构造函数不能应用于给定的类型

我是java新手我对inheritance或构造函数知之甚少。 扩展类时是否有使用构造函数的限制(如参数数量)? 这是我的计划: class Box { int length,breadth,height; Box(int l,int b,int h) { length=l; breadth=b; height=h; } int volume() { return length*breadth*height; } } class BoxWeight extends Box { int weight; BoxWeight(int l,int b,int h,int w) { length=l; breadth=b; height=h; weight=w; } } class BoxInheritance { public static void main(String args[]) { Box B1=new Box(5,75,4); […]

为什么它在子类对象中为超类变量存储或分配内存?

在以下代码中 – class Mammal { String name = “furry “; String makeNoise() { return “generic noise”; } } class Zebra extends Mammal { String name = “stripes “; String makeNoise() { return “bray”; } } public class ZooKeeper { public static void main(String[] args) { new ZooKeeper().go(); } void go() { Mammal m = new […]

为什么一个类不能扩展枚举?

我想知道为什么在Java语言中一个class不能扩展enum 。 我不是在谈论扩展enum (由于java没有多重inheritance,而且enum隐式地扩展了java.lang.Enum ),因此无法完成enum ,但是按顺序extends enum的类只添加额外的方法,而不是额外的枚举值。 就像是: enum MyEnum { ASD(5), QWE(3), ZXC(7); private int number; private asd(int number) { this.number=number; } public int myMethod() { return this.number; } } class MyClass extends MyEnum { public int anotherMethod() { return this.myMethod()+1; } } 要像这样使用: System.out.println(MyClass.ASD.anotherMethod()); 那么,任何人都可以为此限制提供理由(或指向正确的JLS部分)吗?

不能在运行时在java中向下转发

我有一个类Animal和一个类Dog如下: class Animal{} class Dog extend Animal{} 主要课程: class Test{ public static void main(String[] args){ Animal a= new Animal(); Dog dog = (Dog)a; } } 错误显示: Exception in thread “main” java.lang.ClassCastException: com.example.Animal cannot be cast to com.example.Dog