為什麼 Java 需要接口
在本課中,我們將討論 Java 中的一個重要概念:接口。你可能對這個詞很熟悉。例如,界面是大多數計算機程序和遊戲的一部分。從廣義上講,界面是一種連接兩個交互方的“控制面板”。請參閱課程以了解該詞在 Java 中的確切含義。
有用的材料
正如這個簡短系列文章的作者所寫,這些材料將使那些已經弄清楚如何創建接口、了解接口繼承的工作原理並實現了幾個示例但仍然不明白原因的人受益。下面的材料介紹了接口的“應用程序”:
抽像類和接口的區別
在本課中,您將了解抽像類與接口的不同之處,並且您將看到常見抽像類的示例。
抽像類和接口之間的區別很重要。在你未來 90% 的工作面試中,你會被問及這些概念之間的區別。因此,請確保您理解所閱讀的內容。
Java求職面試中關於抽像類和接口的10個問題
在本文中,我們將研究在各個級別的 Java 軟件開發職位的面試中最常見的與抽像類和接口相關的問題。即使是新手 Java 程序員,其中的大部分內容也很清楚。它們主要是開放式問題,但其中一些可能很棘手,例如解釋 Java 中抽像類和接口之間的區別,或者解釋何時更喜歡抽像類而不是接口。
8
任務
Java Core, 等級 4, 課堂 8
Bridges
Create a Bridge interface with a getCarsCount() method that counts cars. Write WaterBridge and SuspensionBridge classes that implement this interface. The getCarsCount() method must return any constant int value. This value must be different for different classes.
8
任務
Java Core, 等級 4, 課堂 8
Wine tasting
30% of the robots on Planet Soto have an alcohol dependence, which causes anxiety among the remaining 70%. More precisely, the 30% are addicted to alcohol-related programs. We are talking about robots, after all. But now it's your chance to do the same!
16
任務
Java Core, 等級 4, 課堂 8
User, loser, coder and programmer
1. Enter a few strings (keys) from the keyboard [in a loop].
The following strings (keys) are allowed: "user", "loser", "coder", "programmer".
Stop reading in strings after reading in any string that does not match one of the above.
2. For each entered string:
2.1. Create the appropriate object [see Person.java], e.g.
4
任務
Java Core, 等級 4, 課堂 8
Implement the printMainInfo method
Write an implementation of the printMainInfo method:
* If a Drawable object is passed to the method, call the draw method on the object.
* If a Movable object is passed to the method, call the move method on the object.
8
任務
Java Core, 等級 4, 課堂 8
Computer
We're using programming and OOP tools to build a new computer for work. We won't just have a Computer class. We'll also have a CompItem interface that will be implemented by the Computer class.
16
任務
Java Core, 等級 4, 課堂 8
MovieFactory
You have the code for a MovieFactory program. Figure out what it does. Add Cartoon and Thriller classes. Understand how we get a SoapOpera object using the key "soapOpera".
8
任務
Java Core, 等級 4, 課堂 8
Janitorial services
Let's create a virtual janitorial business with a cleanAllApartments method for all apartment types and individual methods for cleaning one-, two- and three-bedroom apartments.
8
任務
Java Core, 等級 4, 課堂 8
Fixing bugs
Think about how CanSwim and CanWalk interfaces relate to the SeaCreature class. Create the correct inheritance relationship between the interfaces and the SeaCreature class. And how might the Orca, Whale, and RiverOtter classes relate to the SeaCreature class?
16
任務
Java Core, 等級 4, 課堂 8
Currencies
Let's do some work with money. Your job is to expand the functionality of a program that can manipulate currencies. For the specific currencies, see the task conditions. To start, create a private double field amount in the abstract Money class...
8
任務
Java Core, 等級 4, 課堂 8
Fix four mistakes
This code has four bugs, and your task is... Yeah, you already know what your task is. First of all, you need to figure out what the code does. Create something and fix the bugs that were written before you got involved with the code (as well as any that appear after you get involved).
8
任務
Java Core, 等級 4, 課堂 8
Exception invasion
In reality, there are oceans of exceptions in programs, and you'll eventually get used to them. You can start right now: populate the list exceptions with ten (10) different exceptions. The first exception is already implemented in the initExceptions method.
16
任務
Java Core, 等級 4, 課堂 8
GCD
We continue our attempts to enter the cosmic space belonging to Planet Linear Chaos. Here's another task taken from a border-crossing interview, for young arrivals: find the greatest common divisor (GCD) of two positive integers.
8
任務
Java Core, 等級 4, 課堂 8
Singleton
A class implements the Singleton pattern if it lets you create just one object of its type. You need to create a Singleton class in a separate file. Add a static getInstance method that returns the same object any time it is called. Think about how you can prevent the creation of additional instances of this class.