I want to simulate Smart Devices world. My plan was to create a hierarchy like this.
Smart Device as my main interface. Phone and Watch as classes which implement Smart Device. and Samsung and Apple as classes which extend Phone.
and finally I have two classes named ios and android which extend a superclass named Os.
I want to have the ability to enforce apple and samsung to install their own respective Os while I enforce the installation of Os on every Device(such as watch) which implements SmartDevice.
Therefore I created a method called istallOs(Os test) in my interface but the problem is I can not implement that method in phone level because I don't know if it is samsung or apple.
I am guessing my Hierarchy is not suitable for this problem and I should move installOs to a lower level.
What is the solution to this problem? If you can include bounded Types and a sample in your solution that would be great.
public interface SmartDevice<T extends Os> {
public Boolean installOs(T os);
}
public abstract class Phone implements SmartDevice<T extends Os>{
@Override
public Boolean installOs(T t) {
//Cant implement it here since I dont know what device i am dealing with
}
}
public class ApplePhone extends Phone<ios> {
@Override
public Boolean installOs(ios os) {
//this is an Apple phone so its ok to install ios in it.
System.out.println("Installing Os: "+os.getName());
return true;
}
}
I just found out that I could put an abstract method in my interface!
so all I needed all along was
public interface SmartDevice<T extends Os> {
public abstract Boolean installOs(T os);
}
Then I went to the phone class and changed bounded types to this:
public abstract class Phone<A extends Os> implements SmartDevice<A>{
@Override
public abstract Boolean installOs(A s) ;
}
Thank you for your help guys.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.