在java中,接口仅包含方法类型,名称和参数.实际的实现是在实现它的类中完成的.鉴于此,如何创建一个接口实例并将其用作类对象?有许多这样的接口,例如org.w3c.dom.Node.
这是我正在使用的代码:
DocumentBuilderFactory fty = DocumentBuilderFactory.newInstance();
fty.setNamespaceAware(true);
DocumentBuilder builder = fty.newDocumentBuilder();
ByteArrayInputStream bais = new ByteArrayInputStream(result.getBytes());
Document xmldoc = builder.parse(bais);
NodeList rm1 = xmldoc.getElementsByTagName("Subject");
Node rm3 = rm1.item(0);
Run Code Online (Sandbox Code Playgroud) 我正在浏览一些接口,我想知道为什么这不起作用:
interface I {
public void doSomething(String x);
}
class MyType implements I {
public int doSomething(String x) {
System.out.println(x);
return(0);
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,为什么我不能在界面中实现该方法?有一个返回类型有不同的签名吗?是不是名称,参数和返回类型使方法独特?
我有一个"页面",它实现了我自己的"权限"界面.
public interface PagePermissions{
Dictionary<string, Permission> readPermissions();
}
public partial class myWebPage: System.Web.UI.Page, PagePermissions
{
protected void Page_Load(object sender, EventArgs e)
{
}
Dictionary<string, Permission> PagePermissions.readPermissions()
{
Dictionary<string, Permission> results = new Dictionary<string, Permission>();
return results;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的母版页中,我获得了对当前Page对象的引用.
Page myPage = HttpContext.Current.Handler as Page;
Run Code Online (Sandbox Code Playgroud)
但我无法调用我的函数ReadPermission,因为它没有识别它:
myPage.readPermissions();
Run Code Online (Sandbox Code Playgroud)
如何调用我实现的功能?
我想创建一个在Java中实现它自己的一些方法的接口(但是语言不允许这样做,如下所示):
//Java-style pseudo-code
public interface Square {
//Implement a method in the interface itself
public int getSize(){//this can't be done in Java; can it be done in C++?
//inherited by every class that implements getWidth()
//and getHeight()
return getWidth()*getHeight();
}
public int getHeight();
public int getWidth();
}
//again, this is Java-style psuedocode
public class Square1 implements Square{
//getSize should return this.getWidth()*this.getHeight(), as implemented below
public int getHeight(){
//method body goes here
}
public int getWidth{
//method body goes here
} …Run Code Online (Sandbox Code Playgroud) 我有以下界面:
interface MySortedCollection<T extends Comparable<T>> {
boolean isElement(T t);
void insert(T t);
void printSorted();
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用AVLTree来实现接口:
public class AVLTree<T> implements MySortedCollection{
private AVLNode<T> tree=null;
public AVLTree (){
}
public boolean isElement(T t){
}
public void insert(T t){
if(tree==null){
tree= new AVLNode<T>(t);
}
}
public void printSorted(){}
}
Run Code Online (Sandbox Code Playgroud)
但是我得到了错误:
error: AVLTree is not abstract and does not override abstract
method insert(Comparable) in MySortedCollection
public class AVLTree<T> implements MySortedCollection{
Run Code Online (Sandbox Code Playgroud)
怎么了?
通过介绍,我正在为个人学习目的创建一个基本的Quadtree引擎.我希望这个引擎能够处理许多不同类型的形状(目前我正在使用圆形和方形),这些形状将在窗口中移动并在发生碰撞时执行某种操作.
在先前询问关于通用列表主题的问题后,我决定使用多态性接口.最好的界面是一个界面利用,Vector2因为我的四叉树中出现的每个对象都有一个x,y位置,Vector2并且很好地覆盖了它.这是我目前的代码:
public interface ISpatialNode {
Vector2 position { get; set; }
}
public class QShape {
public string colour { get; set; }
}
public class QCircle : QShape, ISpatialNode {
public int radius;
public Vector2 position {
get { return position; }
set { position = value; }
}
public QCircle(int theRadius, float theX, float theY, string theColour) {
this.radius = theRadius;
this.position = new Vector2(theX, theY);
this.colour = theColour;
}
}
public class …Run Code Online (Sandbox Code Playgroud) 我有几个类在子类型层次结构中是相同的"级别".我需要为每个实例创建一个ID,我通常通过拥有一个包含静态long的父类来实现,我只是为子类的每个实例增加它.
75%的子类型代码是相同的 - 这使我更喜欢使用抽象类(通过接口):
1)我可以为ID创建者声明一个静态变量
2)我可以将代码放在父类中,并在子类型中共享它
它是否正确?我似乎很少使用接口.当子类型需要相同的方法,但不同的实现时,以及当我不需要初始化要在子类型(如ID创建者)之间共享的属性时,我应该只使用接口吗?
我一直给同事们的印象是Interfaces比继承更受欢迎.
我正在尝试编写一个看起来像这样的界面
public interface IPropertyGroupCollection
{
IEnumerable<IPropertyGroup> _Propertygroups { get;}
}
public interface IPropertyGroup
{
IEnumerable<IProperty<T, U, V>> _conditions { get; }
}
public interface IProperty<T, U, V>
{
T _p1 { get; }
U _p2 { get; }
V _p3 { get; }
}
public class Property<T, U, V> : IProperty<T, U, V>
{
//Some Implementation
}
Run Code Online (Sandbox Code Playgroud)
我继续为_Conditions的可枚举定义获取编译错误.
我究竟做错了什么?Idea是实现类将提供通用属性包集合
I've come to a point in a Go project of mine where I'd like to create multiple subclasses of a base class, and be able to operate on instances of the subclasses through a base class/interface variable (I'm using the word "class" even though the concept doesn't really exist in Go).
Here's what it might look like in C++ just to show what I mean:
#include <iostream>
using namespace std;
class Base {
public:
int x,y;
virtual void DoStuff() {}; …Run Code Online (Sandbox Code Playgroud) 我有一个实现两个接口的抽象类.我正确的思考因为我使用两个接口,我不能使用任何一个接口来实现动态绑定?原因是,如果我使用的接口之一,我显然不能够与其他接口类型系统将只允许子类型调用由我来宣布的多态变量的接口中定义的方法调用的方法呢?
因此,我的实际问题是,我只是真的使用接口来确保我的抽象类(或子类)明确提供方法的实现吗?这似乎与第19条规定的内容相矛盾 - 你应该只使用类型的接口(我认为这意味着多态).
例:
public interface A{
public void meth1();
}
public interface B{
public void meth2();
}
public abstract class C implements A,B{
}
public void DynamicBinding(A aobject){
//Can only call aobject.meth1();
}
Run Code Online (Sandbox Code Playgroud) interface ×10
java ×6
c# ×3
inheritance ×2
asp.net ×1
comparable ×1
generics ×1
go ×1
methods ×1
oop ×1
polymorphism ×1
quadtree ×1
struct ×1
text-files ×1