मैं प्रोग्रामिंग कक्षा में मेरे लिए होमवर्क असाइनमेंट पर काम कर रहा हूं जिसमें इंटरफेस को लागू करना शामिल है। यहां समस्या यह है कि मैं वास्तव में बस फ्लैट आउट इंटरफेस को समझ नहीं पा रहा हूं या उनके लिए क्या उपयोग किया जाता है (प्रोफेसर इसे समझाने के बारे में बहुत अच्छा नहीं था)।जावा - कार्यान्वयन इंटरफेस
असाइनमेंट "वाहन" सुपरक्लास, और तीन उप-वर्गों, "ट्रक" या "जीप" जैसे कुछ बनाने के लिए था, जिसमें प्रत्येक के अपने कुछ लक्षण होंगे। "वाहन" वर्ग को तुलनीय इंटरफ़ेस को कार्यान्वित करना होगा, जो मुझे लगता है कि मैंने पाया है (मेरे पास compareTo()
वाहनों पर दरवाजे की संख्या की तुलना में लिखी गई विधि है), और एक अन्य वर्ग को "हाइब्रिड" कक्षा को भी लागू करना होगा (मेरे पास नहीं है विचार करें इसका क्या अर्थ है)। इसके बाद हमें toString()
, equals(Object o)
, और compareTo(Object o)
विधियों को लागू करना होगा।
मुझे लगता है कि मेरे पास compareTo()
नीचे है, लेकिन equals()
के साथ मुझे कोई जानकारी नहीं है। आखिरी चीज जो हमें करना है वह परीक्षण करने के लिए मुख्य लिखना है, इसमें वाहन वस्तुओं की एक सरणी बनाना, उन्हें प्रिंट करना, उन्हें छंटाई करना, और फिर उन्हें प्रिंट करना शामिल है। इसे सरणी के माध्यम से फिर से शुरू करना चाहिए और हाइब्रिड के लिए मूल्य प्रीमियम प्रिंट करना चाहिए, और 2 वाहनों की तुलना करने के लिए equals(Object o)
विधि का उपयोग करना चाहिए।
यहाँ मेरी "वाहन" सुपर क्लास
package vehicle;
abstract public class Vehicle implements Comparable {
private String color;
private int numberOfDoors;
// Constructor
/**
* Creates a vehicle with a color and number of doors
* @param aColor The color of the vehicle
* @param aNumberOfDoors The number of doors
*/
public Vehicle(String aColor, int aNumberOfDoors) {
this.color = aColor;
this.numberOfDoors = aNumberOfDoors;
}
// Getters
/**
* Gets the color of the vehicle
* @return The color of the vehicle
*/
public String getColor() {return(this.color);}
/**
* Gets the number of doors the vehicle has
* @return The number of doors the vehicle has
*/
public int getNumberOfDoors() {return(this.numberOfDoors);}
// Setters
/**
* Sets the color of the vehicle
* @param colorSet The color of the vehicle
*/
public void setColor(String colorSet) {this.color = colorSet;}
/**
* Sets the number of doors for the vehicle
* @param numberOfDoorsSet The number of doors to be set to the vehicle
*/
public void setNumberOfDoors(int numberOfDoorsSet) {this.numberOfDoors = numberOfDoorsSet;}
@Override
public int compareTo(Object o) {
if (o instanceof Vehicle) {
Vehicle v = (Vehicle)o;
return this.numberOfDoors - v.getNumberOfDoors();
}
else {
return 0;
}
}
/**
* Returns a short string describing the vehicle
* @return a description of the vehicle
*/
@Override
public String toString() {
String answer = "The car's color is "+this.color
+". The number of doors is"+this.numberOfDoors;
return answer;
}
}
के लिए कोड है और मैं भी मेरी उपवर्गों
package vehicle;
abstract public class Convertible extends Vehicle {
private int topSpeed;
// Constructor
/**
* Creates a convertible with a color, number of doors, and top speed
* @param aColor The color of the convertible
* @param aNumberOfDoors The number of doors of the convertible
* @param aTopSpeed The top speed of the convertible
*/
public Convertible (String aColor, int aNumberOfDoors, int aTopSpeed) {
super(aColor, aNumberOfDoors);
this.topSpeed = aTopSpeed;
}
// Getters
/**
* Gets the top speed of the convertible
* @return The top speed of the convertible
*/
public int getTopSpeed() {return(this.topSpeed);}
// Setters
/**
* Sets the top speed of the convertible
* @param topSpeedSet The top speed to set to the convertible
*/
public void setTopSpeed(int topSpeedSet) {this.topSpeed = topSpeedSet;}
/**
* Returns a short description of the convertible
* @return a short description of the convertible
*/
@Override
public String toString() {
String answer = "The car's color is "+super.getColor()
+", the number of doors is "+super.getNumberOfDoors()
+", and the top speed is "+this.topSpeed+" mph.";
return answer;
}
}
मैं मेरे लिए काम करने के लिए किसी को भी पूछ निश्चित रूप से नहीं कर रहा हूँ में से एक पोस्ट करेंगे, लेकिन अगर कोई इंटरफेस कैसे काम करता है और यह होमवर्क वास्तव में क्या पूछ रहा है, तो यह थोड़ा और अधिक प्रकाश डाल सकता है।
सभी मदद की बहुत सराहना की जाती है
धन्यवाद!
एक इंटरफेस मूल रूप से कक्षा के लिए एक अनुबंध है। यह कहता है कि यदि आप खुद को 'तुलनात्मक' कहना चाहते हैं तो आपको इन विधियों को लागू करने की आवश्यकता है जो 'तुलनात्मक' बनाते हैं। –
एक इंटरफेस एक मानक तरीका देखने और व्यवहार करने के लिए कक्षाओं को मजबूर करने का एक तरीका है। जब कोई इंटरफ़ेस किसी विधि को परिभाषित करता है, तो उस इंटरफ़ेस को लागू करने वाली प्रत्येक कक्षा को उस विधि को ओवरराइड करना होगा, जो गारंटी देता है कि उस वर्ग के उस विधि का अपना संस्करण होगा। – Hassan
मुझे आशा है कि आपका प्रोफेसर इस प्रश्न को नहीं पढ़ेगा। :) – Dhananjay