当我们扩展一个类并实现一个接口时,我们把它写成 -
public interface AnyInterface{ ---- }
public class Base{ -- }
public class Child extends Base implements AnyInterface{ ---- }
Run Code Online (Sandbox Code Playgroud)
现在我的问题是为什么我们不能把它写成 -
public class child implements AnyInterface extends Base{ --- }
Run Code Online (Sandbox Code Playgroud) public interface MemcachedAccessor {
void set(String key, Object value, int cacheTime, long timeout,
TimeUnit timeUnit);
Object get(String key);
}
Run Code Online (Sandbox Code Playgroud)
我有一个接口:MemcachedAccessor和一个实现:MemcachedAccessorImpl.
哪种名称风格更好?
MemcachedAccessor 和 MemcachedAccessorImpl
要么
MemcachedAccess 和 MemcachedAccessImpl
?
任何规则?
ActionListener是一个接口,但为什么我可以创建实例对象?
JButton button = new JButton("Button1");
ActionListener me = new ActionListener(){
public void actionPerformed(ActionEvent ae){
JOptionPane.showMessageDialog(null,ae.getActionCommand());
}
};
button.addActionListener(me);
Run Code Online (Sandbox Code Playgroud)
还有什么?我不确定.请帮我.
我正在尝试声明一个必须实现特定接口的对象.我认为以下内容可以在Java中使用,就像在其他一些语言中一样,但我在这里不知所措:
Class<? implements **theInterface**> implementingObject
Run Code Online (Sandbox Code Playgroud)
任何指针将不胜感激
在接口与抽象类的问题和接受的答案的刺激下,我希望得到一个更详细和澄清的答案.特别是我无法理解语句"接口中的字段是隐式静态和最终的".这是否意味着实现包含方法foo()的接口的类A可以调用该方法A.foo()?
关于final的内容:只要接口只包含方法,给定一个实现带方法接口的抽象类A foo()和class B扩展它的普通接口abstract class A,不能class B覆盖foo方法吗?就我而言,最终的方法是不可能被覆盖的.最后真的是什么?
编写名为Nameable的接口,指定以下方法:
public void setName(String n)
public String getName()
Run Code Online (Sandbox Code Playgroud)
这就是我得到的:
public interface Nameable{
public void setName(String n){
n =name; }
public String getName() {
return n; } }
Run Code Online (Sandbox Code Playgroud)
这是正确的还是有更好的方法吗?
我希望有一个接口指定该接口的任何实现必须在其方法声明中使用特定接口的子类型:
interface IModel {} // The original type
interface IMapper {
void Create(IModel model); // The interface method
}
Run Code Online (Sandbox Code Playgroud)
所以,现在我想我实现这个接口来没有想到IModel本身,而是一个亚型的IModel:
public class Customer : IModel {} // My subtype
public class CustomerMapper : IMapper {
public void Create(Customer customer) {} // Implementation using the subtype
}
Run Code Online (Sandbox Code Playgroud)
目前我收到以下错误:
'CustomerMapper'没有实现接口成员'IMapper.Create(IModel)'
有没有办法实现这个目标?
[编辑]
我再次组织我的问题,
参数模型
public class PaymentModel
{
...
}
public class CCPaymentModel : PaymentModel
{
...
}
public class PaypalPaymentModel : PaymentModel
{
...
}
public class GooglePaymentModel : PaymentModel
{
...
}
Run Code Online (Sandbox Code Playgroud)
接口类
public interface IPayment<T> where T : PaymentModel
{
...
}
Run Code Online (Sandbox Code Playgroud)
模型(从IPayment获得继承),
public class SagePayment
: IPayment<CreditCardPaymentInfo>
{
public void MakePayment( CreditCardPaymentInfo creditCardPaymentInfo ) {
// ...
}
public void MakeRefund( CreditCardPaymentInfo creditCardPaymentInfo ) {
// ...
}
}
public class GooglePayment
: IPayment<GooglePaymentModel>
{
public void …Run Code Online (Sandbox Code Playgroud) public interface ICalculator {
public double multi(double a, double b);
public int add(int a, int b);
public int sub(int a, int b);
}
public class Calculator extends ICalculator {
protected int add(double a, double b) {
return a+b;
}
public double sub(int zahl1, int zahl2 ) {
return a*b;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我不能在类Calculator中使用受保护的方法?我的回答是"protected"在同一个类和子类中很有用.我也不能认为Interface中实现的类中的方法也是继承的,就像子类一样.
class A implements Serializable{
}
class B extends A{
//this also inherits the marker
}
Run Code Online (Sandbox Code Playgroud)
那么有没有办法在课堂上实现/取消标记B?
示例:如果有两个类扩展A名为B和C.但是他们只C想要序列化而不是B.
interface ×10
java ×8
c# ×2
class ×2
generics ×2
inheritance ×2
final ×1
implements ×1
methods ×1
oop ×1
polymorphism ×1
static ×1