英文:
Can a subclass implementing an interface in java have its own methods?
问题
例如,我有以下的代码:
Shape.java
public interface Shape{
double getArea();
}
Circle.java
public class Circle implements Shape {
private double radius;
public Circle(){
radius = 1.0;
}
public Circle(double radius){
this.radius = radius;
}
@Override
public String toString() {
return "Circle[radius=" + radius + "]";
}
@Override
public double getArea(){
return 3.14 * radius * radius;
}
public double getCircumference(){
return 2 * 3.14 * radius;
}
}
Rectangle.java
public class Rectangle implements Shape{
private double length, width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public String toString() {
return "Rectangle[length=" + length + ",width=" + width + "]";
}
@Override
public double getArea() {
return length * width;
}
}
以及 TestShape.java 文件
public class TestShape {
public static void main(String[] args){
Shape s1 = new Circle(2.0);
System.out.println(s1.getCircumference());
}
}
现在,我想让 Circle 类有一个 getCircumference() 方法,但是会遇到 cannot find symbol 错误。是否不可能在实现接口的子类中添加额外的方法?如果有可能,该如何实现呢?
英文:
For Example, I have this code that goes like
Shape.java
public interface Shape{
double getArea();
}
Circle.java
public class Circle implements Shape {
private double radius;
public Circle(){
radius = 1.0;
}
public Circle(double radius){
this.radius = radius;
}
@Override
public String toString() {
return "Circle[radius=" + radius + "]";
}
@Override
public double getArea(){
return 3.14 * radius * radius;
}
}
Rectangle.java
public class Rectangle implements Shape{
private double length, width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public String toString() {
return "Rectangle[length=" + length + ",width=" + width + "]";
}
@Override
public double getArea() {
return length * width;
}
}
And the TestShape.java file
public class TestShape {
public static void main(String[] args){
Shape s1 = new Circle(2.0);
System.out.println(s1.getCircumference());
}
}
Now, I want the Circle class to have a getCircumference() method but it is running into a cannot find symbol error. Is it not possible to add extra methods to a subclass implementing an interface, and if there is, how to do that?
Edit: Deleted the getPerimeter() method from Rectangle class. It also lead to the same error by the way, on trying to call it.
答案1
得分: 2
你可以在已实现任何接口的类中添加任意多的方法,但在使用接口引用访问方法时,只能访问接口中定义的方法。
Shape s = new Circle();
// 编译器看到了 Shape 接口的引用,它没有 `getCircumference()` 函数
如果你想要访问类中的方法,你需要使用类的引用
Circle s = new Circle();
英文:
You can add as many methods as you want in a class which has already implemented any interface or interfaces but you are accessing the method using interface reference
Shape s = new Circle();
// So compiler see the reference of Shape interface and it did not have any `getCircumfrence() function
If you want to access the class method you need to use class reference
Circle s = new Circle()
答案2
得分: 0
是的,这是可能的。只需在类Circle
中添加一个方法getCircumference()
,您就可以得到它。
英文:
Yes, it's possible. Just add a method getCircumference()
to class Circle
and you will have it.
答案3
得分: 0
你在 Rectangle.java
中已经有一个名为 getPerimeter()
的方法,类似地,你可以在 Circle.java
中创建一个名为 getCircumference()
的方法。
英文:
You already have a method in Rectangle.java
named getPerimeter()
, similarly you can create a method named getCircumference()
in Circle.java
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论