界面如何工作?

编程入门 行业动态 更新时间:2024-10-13 14:26:52
本文介绍了界面如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是OOP概念的新手,我希望收到一些有关此事的澄清。可以在类中设置对象的交互,所以我的问题是当蓝图中已经建立了方法时创建接口的目的是什么? 实现:

package objectOrientedProgrammingConcepts; 公共舱门{ String state =shut; void changeState(String newState){ state = newState; } void printState(){ System.out.println(门是+州+。); } }

对象创建:

package objectOrientedProgrammingConcepts; 公共类DoorDemo { public static void main(String [] args){ Door door1 = new Door(); 门门2 =新门(); door1.changeState(open); door2.changeState(关闭); door1.printState(); door2.printState(); } }

我的尝试: - Java的文档 - 课程:面向对象的编程概念(Java™教程>学习Java语言) [ ^ ]

解决方案

这里需要了解的是: 1.继承 2.多态性 然后在高级概念中,您还需要了解如何开发API。可能在大多数情况下,在需要定义合同结构的地方使用接口 - 在API中,尤其是在使用接口等的地方,您定义合同而不是简单对象。原因是,必须实现接口。该接口可用于确保客户端应用程序使用我们的API编写了我们的标准实现 - 结构等。 接口不仅用于Java,它们也用在C#和C ++中(尽管C ++不提供任何关键字接口)。在开发客户端应用程序时应用它们的逻辑用法,我不会在普通应用程序中使用(也不建议)考虑它们。 需要一个例子吗?当然,阅读Oracle文档中的教程,它们提供了一个非常好的自行车API示例,

interface Bicycle { // 每分钟车轮转数 void changeCadence( int newValue); void changeGear( int newValue); void speedUp( int increment); void applyBrakes( int 减量); }

现在,如果你想创建这个标准的实现,那么你实现了所有的接口, class ACMEBicycle implements Bicycle { int cadence = 0 ; int speed = 0 ; int gear = 1 ; // 编译器现在需要方法 // changeCadence,changeGear,speedUp和applyBrakes // 全部实施。如果此类中缺少 // 方法,则编译将失败。 void changeCadence( int newValue){ cadence = newValue ; } void changeGear( int newValue){ gear = newValue; } void speedUp( int increment){ speed = speed + increment; } void applyBrakes( int 减量){速度=速度 - 减量; } void printStates(){ System.out.println( cadence: + cadence + speed: + speed + gear: + gear); } }

注意界面中的空方法体,以及向类中相同函数添加实体。课堂上还有一件事,你能猜到吗? 我会把剩下的部分留给你,请打开NetBeans并尝试在这里和那里乱七八糟地了解它们是如何工作的。

I am new to the OOP concept and I wish to receive some clarification on the matter. The interaction of an object can be set in the class and so my question is what is the purpose of creating an interface when methods are already established in the blueprint? Implementation:

package objectOrientedProgrammingConcepts; public class Door { String state = "shut"; void changeState(String newState) { state = newState; } void printState() { System.out.println("The door is " + state + "."); } }

Object Creation:

package objectOrientedProgrammingConcepts; public class DoorDemo { public static void main(String[] args) { Door door1 = new Door(); Door door2 = new Door(); door1.changeState("open"); door2.changeState("shut"); door1.printState(); door2.printState(); } }

What I have tried: - Java's Documentation - Lesson: Object-Oriented Programming Concepts (The Java™ Tutorials > Learning the Java Language)[^]

解决方案

What you need to learn here are: 1. Inheritance 2. Polymorphism And then later on, in the advanced concepts, you also need to understand how an API is developed. Probably, in most cases, an interface is used where you need to define a structure of your contract — in APIs, especially where you are using interfaces etc, you define contracts, instead of simple objects. The reason is, an interface must be implemented. That interface, can be used to ensure that the client applications, using our API, has written an implementation of our standard — structure etc. Interfaces are not only used in Java, they are also used in C# and C++ (although, C++ does not provide any keyword interface). The logical usage of them is applied when you are developing a client application, I would not use (nor recommend) considering them in a plain application. Need an example? Sure, read the tutorial in the Oracle documents, they provide a very good example of a Bicycle API,

interface Bicycle { // wheel revolutions per minute void changeCadence(int newValue); void changeGear(int newValue); void speedUp(int increment); void applyBrakes(int decrement); }

Now, if you want to create an implementation of this standard, then you implement all the interface itself,

class ACMEBicycle implements Bicycle { int cadence = 0; int speed = 0; int gear = 1; // The compiler will now require that methods // changeCadence, changeGear, speedUp, and applyBrakes // all be implemented. Compilation will fail if those // methods are missing from this class. void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { gear = newValue; } void speedUp(int increment) { speed = speed + increment; } void applyBrakes(int decrement) { speed = speed - decrement; } void printStates() { System.out.println("cadence:" + cadence + " speed:" + speed + " gear:" + gear); } }

Notice the empty method bodies in the interface, and the addition of bodies to the same functions in the class. There is one more extra thing in the class, can you guess that? I would leave the rest up to you, please also open up NetBeans and try to mess around here and there with a few things to understand how they work.

更多推荐

界面如何工作?

本文发布于:2023-11-28 13:39:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642628.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:界面   工作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!