如何实现接口(如何实现接口的方法)

网友投稿 203 2022-12-31


本篇文章给大家谈谈如何实现接口,以及如何实现接口的方法对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。 今天给各位分享如何实现接口的知识,其中也会对如何实现接口的方法进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

Java中的接口怎么实现?

举个面积的例子:在java中如何实现接口,定义一个接口如何实现接口,声明计算长方形面积和周长的抽象方法,再用一个类去实现这个接口,再编写一个测试类去使用这个接口。首先,接口必须单独存放,如果我们用eclipse编程的话,它们提示:The public type **** must be defined in its own file,意思是必须要定义在其自己的文件中,所以要为接口文件单独存放起来,举例,我们的接口要实现获到矩形的长,宽,面积,周长,所以定义以下的接口。public interface calrect {\x0d\x0apublic abstract int calarea();\x0d\x0apublic abstract int calgirth();\x0d\x0apublic abstract int getx();\x0d\x0apublic abstract int gety();\x0d\x0a}注意,定义接口就像定义类一样,接口的访问控制符只能用public,用public定义的接口可以被所有的类和包引用,而缺省的则只能被同一个包中的其他类和接口引用,这符合JAVA中访问控制符的一般要求,关于接口再引用其他接口则是后话。以上接口文件名为calrect.java.另外需要指出的是接口中不能给方法给出方法体。接下来,需要定义一个类来实现接口,因为不知道JAVA的内置矩形类是什么名,所以为了安全,将该类定义为RRect,这可以认为是一种安全策略。关于implements,可以参考其他资料。该类引用了接口calrect,所以必须对calrect中的方法一一实现。//定义矩形类 应用接口class RRect implements calrect{private int x;\x0d\x0aprivate int y;public RRect (){\x0d\x0a x=3;y=4;\x0d\x0a}\x0d\x0apublic int calarea(){\x0d\x0a return x*y;\x0d\x0a}\x0d\x0apublic int calgirth(){\x0d\x0a return x*2+y*2;\x0d\x0a}\x0d\x0apublic int getx(){\x0d\x0a return x;\x0d\x0a}\x0d\x0apublic int gety(){\x0d\x0a return y;\x0d\x0a}\x0d\x0a}//接下来,定义一个测试类,所谓测试类,我理解为定义一个类,在其定义类RRect的对象,并验证其中的方法,看看是不是可以正常使用//定义Class1类\x0d\x0apublic class Class1{\x0d\x0aRRect rect;\x0d\x0apublic static void main(String []args){\x0d\x0a RRect rect=new RRect();\x0d\x0a System.out.println("矩阵的长"+ rect.getx());\x0d\x0a System.out.println("矩阵的宽"+ rect.calarea());\x0d\x0a System.out.println("矩阵的面积"+ rect.calarea());\x0d\x0a System.out.println("矩形的周长 "+rect.calgirth());\x0d\x0a}\x0d\x0a\x0d\x0a}运行结果:矩阵的长3\x0d\x0a矩阵的宽12\x0d\x0a矩阵的面积12\x0d\x0a矩形的周长 14注:接口单存放,接口实现类和测试类可以存放在一个文件中

Java中的接口是什么?如何实现?

Java接口是一系列方法如何实现接口的声明如何实现接口,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为(功能)。接口可以定义类型,但不能直接实例化,必须通过类来实现接口方法,然后通过类的实例化对象来使用接口。
下面如何实现接口我们声明一个接口:
interface Animal {
public void eat(); //在接口中,不需实现这些方法。
public void travel();
}
用类实现接口中的方法:
public class MammalInt implements Animal{
public void eat(){
System.out.println("Mammal eats");
}
public void travel(){
System.out.println("Mammal travels");
}
}
对于接口和实现的问题,你可以在秒秒学如何实现接口了解下。

Javascript如何实现接口?

在javascript中并没有原生的创建或者实现接口的方式,或者判定一个类型是否实现了某个接口,我们只能利用js的灵活性的特点,模拟接口。
在javascript中实现接口有三种方式:注释描述、属性验证、鸭子模型。
note:因为我看的是英文书,翻译水平有限,不知道有些词汇如何翻译,大家只能领会精神了。
1. 注释描述 (Describing Interfaces with Comments)
例子:
复制代码 代码如下:
/*
interface Composite {
function add(child);
function remove(child);
function getChild(index);
}
interface FormItem {
function save();
}
*/
var CompositeForm = function(id, method, action) { // implements Composite, FormItem
...
};
//Implement the Composite interface.
CompositeForm.prototype.add = function(child) {
...
};
CompositeForm.prototype.remove = function(child) {
...
};
CompositeForm.prototype.getChild = function(index) {
...
};
// Implement the FormItem interface.
CompositeForm.prototype.save = function() {
...
};
模拟其他面向对象语言,使用interface 和 implements关键字,但是需要将他们注释起来,这样就不会有语法错误。
这样做的目的,只是为了告诉其他编程人员,这些类需要实现什么方法,需要在编程的时候加以注意。但是没有提供一种验证方式,这些类是否正确实现了这些接口中的方法,这种方式就是一种文档化的作法。
2. 属性验证(Emulating Interfaces with Attribute Checking)
例子:
复制代码 代码如下:
/* interface
Composite {
function add(child);
function remove(child);
function getChild(index);
}
interface FormItem {
function save();
}
*/
var CompositeForm = function(id, method, action) {
this.implementsInterfaces = ['Composite', 'FormItem'];
...
};
...
function addForm(formInstance) {
if(!implements(formInstance, 'Composite', 'FormItem')) {
throw new Error("Object does not implement a required interface.");
}
...
}
// The implements function, which checks to see if an object declares that it
// implements the required interfaces.
function implements(object) {
for(var i = 1; i < arguments.length; i++) {
// Looping through all arguments
// after the first one.
var interfaceName = arguments[i];
var interfaceFound = false;
for(var j = 0; j < object.implementsInterfaces.length; j++) {
if(object.implementsInterfaces[j] == interfaceName) {
interfaceFound = true;
break;
}
}
if(!interfaceFound) {
return false;
// An interface was not found.
 }
}
return true;
// All interfaces were found.
}
这种方式比第一种方式有所改进,接口的定义仍然以注释的方式实现,但是添加了验证方法,判断一个类型是否实现了某个接口。
3.鸭子类型(Emulating Interfaces with Duck Typing)
复制代码 代码如下:
// Interfaces.
var Composite = new Interface('Composite', ['add', 'remove', 'getChild']);
var FormItem = new Interface('FormItem', ['save']);
// CompositeForm class
var CompositeForm = function(id, method, action) {
...
};
...
function addForm(formInstance) {
ensureImplements(formInstance, Composite, FormItem);
// This function will throw an error if a required method is not implemented.
...
}
// Constructor.
var Interface = function(name, methods) {
if(arguments.length != 2) {
throw new Error("Interface constructor called with "
 + arguments.length + "arguments, but expected exactly 2.");
}
this.name = name;
this.methods = [];
for(var i = 0, len = methods.length; i < len; i++) {
if(typeof methods[i] !== 'string') {
throw new Error("Interface constructor expects method names to be "
+ "passed in as a string.");
}
this.methods.push(methods[i]);
}
};
// Static class method.
Interface.ensureImplements = function(object) {
if(arguments.length < 2) {
throw new Error("Function Interface.ensureImplements called with "
+arguments.length + "arguments, but expected at least 2.");
}
for(var i = 1, len = arguments.length; i < len; i++) {
var interface = arguments[i];
if(interface.constructor !== Interface) {
throw new Error("Function Interface.ensureImplements expects arguments"
+ "two and above to be instances of Interface.");
}
for(var j = 0, methodsLen = interface.methods.length; j < methodsLen; j++) {
var method = interface.methods[j];
if(!object[method] || typeof object[method] !== 'function') {
throw new Error("Function Interface.ensureImplements: object "
+ "does not implement the " + interface.name + " interface. Method " + method + " was not found.");
}
}
}
};
何时使用接口?
一直使用严格的类型验证并不适合,因为大多数javascript程序员已经在没有接口和接口验证的情况下编程多年。当你用设计模式开始设计一个很复杂的系统的时候,使用接口更有益处。看起来使用接口好像限制了javascript的灵活性,但实际上他让你的代码变得更加的松耦合。他使你的代码变得更加灵活,你可以传送任何类型的变量,并且保证他有你想要的方法。有很多场景接口非常适合使用。
在一个大型系统里,很多程序员一起参与开发项目,接口就变得非常必要了。程序员经常要访问一个还没有实现的api,或者为其他程序员提供别人依赖的一个方法存根,在这种情况下,接口变得相当的有价值。他们可以文档化api,并作为编程的契约。当存根被实现的api替换的时候你能立即知道,如果在开发过程中api有所变动,他能被另一个实现该接口的方法无缝替换。
如何使用接口?
首先要解决的问题是,在你的代码中是否适合使用接口。如果是小项目,使用接口会增加代码的复杂度。所以你要确定使用接口的情况下,是否是益处大于弊端。如果要使用接口,下面有几条建议:
1.引用Interface 类到你的页面文件。interface的源文件你可以再如下站点找到: http://jsdesignpatterns.com/.
2.检查你的代码,确定哪些方法需要抽象到接口里面。
3.创建接口对象,没个接口对象里面包含一组相关的方法。
4.移除所有构造器验证,我们将使用第三种接口实现方式,也就是鸭子类型。
5.用Interface.ensureImplements替代构造器验证。
您可能感兴趣的文章:
小议javascript 设计模式 推荐
JavaScript 设计模式之组合模式解析
javascript 设计模式之单体模式 面向对象学习基础
JavaScript 设计模式 安全沙箱模式
JavaScript设计模式之观察者模式(发布者-订阅者模式)
JavaScript设计模式之原型模式(Object.create与prototype)介绍
JavaScript设计模式之工厂方法模式介绍
javascript设计模式之中介者模式Mediator
学习JavaScript设计模式之责任链模式

java怎么使用接口 java如何实现接口操作

接口是Java 实现多继承的一种机制,一个类可以实现一个或多个接口。接口是一系列

方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些

方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为。简单的说接口不

是类,但是定义了一组对类的要求,实现接口的某些类要与接口一致。

在Java 中使用关键字interface 来定义接口。例如:

public interface Compare {
public int compare(Object otherObj);
}

Compare 接口定义了一种操作compare,该操作应当完成与另一个对象进行比较的功能。

它假定某个实现这一接口的类的对象x 在调用该方法时,例如x . compare(y),如果x 小于y,

返回负数,相等返回0,否则返回正数。

举例

public class Student extends People implements Compare{
private String sId; //学号
//Constructor
10
public Student() {
this("","","");
}
public Student(String name,String id,String sId){
super(name,id);
this.sId = sId;
}
public void sayHello(){
super.sayHello();
System.out.println("I am a student of department of computer science.");
}
//get  set method
public String getSId(){
return this.sId;}
public void setSId(String sId){
this.sId = sId;}
//implements Compare interface
public int compare(Object otherObj){
Student other = (Student)otherObj;
return this.sId.compareTo(other.sId);
}
}//end of class

接口的实现

interface ClassName
{
public void getClassName();
}
public class Horse implements ClassName
{
public void getClassName(){
System.out.println("this is Horse's getClassName()!");
}
public static void main(String args[]){
ClassName cn=new Horse();
cn.getClassName();
}
} 关于如何实现接口和如何实现接口的方法的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。 如何实现接口的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于如何实现接口的方法、如何实现接口的信息别忘了在本站进行查找喔。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微服务网关有什么作用(微服务网关的作用)
下一篇:浅谈java.util.concurrent包中的线程池和消息队列
相关文章

 发表评论

暂时没有评论,来抢沙发吧~