我对java继承有一个非常基本的了解。有人可以解释一下应该怎么做?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我对java继承有一个非常基本的了解。有人可以解释一下应该怎么做?相关的知识,希望对你有一定的参考价值。
在我最近的编码轮中提出了这个问题。
我不得不打印以下几行:
My name is Fastfood. My name is Fruits. Our superclass is Food I'm serving Fastfood I'm serving Fruit
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class foodFactory extends Solution{
}
class Food extends Solution{
}
class Solution
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
foodFactory myFoods = new foodFactory();
Food food1 = myFoods.getFood("Fastfood");
Food food2 = myFoods.getFood("Fruits");
System.out.println("My name is: " + food1.getClass().getName());
System.out.println("My name is: " + food2.getClass().getName());
System.out.println("Our superclass is: " + food1.getClass().getSuperclass.getName());
food1.servesFood();
food2.servesFood();
}
}
答案
public class Solution2 {
public static void main(String[] args) {
FoodFactory myFoods = new FoodFactory();
Food food1 = myFoods.getFood("FastFood");
Food food2 = myFoods.getFood("Fruit");
System.out.print("My name is: " + food1.getClass().getName());
System.out.print("My name is: " + food2.getClass().getName());
System.out.print("Our superclass is: " + food1.getClass().getSuperclass().getName());
food1.serveFood();
food2.serveFood();
}
}
class Food {
String name;
public void serveFood() {
// TODO Auto-generated method stub
System.out.print("I'm serving " + name);
}
}
class FoodFactory extends Food {
String name;
public Food getFood(String string) {
if (string.equals("FastFood")) {
Food f = new FastFood();
f.name = string;
return f;
} else {
Food f = new Fruit();
f.name = string;
return f;
}
}
}
class FastFood extends Food {
}
class Fruit extends Food {
}
另一答案
class foodFactory extends HelloWorld{
Food ff = new Food();
public Food getFood(String s){
if(s.equals("Fastfood"))
return new FastFood();
else
return new Fruits();
}
public void servesFood(){
}
}
class Food extends HelloWorld{
public void servesFood(){
System.out.println("I am serving"+this.getClass().getName());
}
}
class Fruits extends Food{}
class FastFood extends Food{}
public class HelloWorld{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
foodFactory myFoods = new foodFactory();
Food food1 = myFoods.getFood("Fastfood");
Food food2 = myFoods.getFood("Fruits");
System.out.println("My name is: " + food1.getClass().getName());
System.out.println("My name is: " + food2.getClass().getName());
System.out.println("Our superclass is: " + food1.getClass().getSuperclass().getName());
food1.servesFood();
food2.servesFood();
}
}
另一答案
我尝试了很多,经过一个小小的修改,我得到了解决方案
class FoodFactory extends Solution
{
public Food getFood(String string)
{
return new Food(string);
}
}
class Food extends Solution
{
String name=null;
public Food(String string)
{
this.name=string;
}
public void servesFood()
{
System.out.println("I'm serving "+this.name);
}
}
class Solution
{
public static void main (String[] args) throws java.lang.Exception
{
FoodFactory myFoods = new FoodFactory();
Food food1 = myFoods.getFood("Fastfood");
Food food2 = myFoods.getFood("Fruits");
System.out.println("My name is: " + food1.name);
System.out.println("My name is: " + food2.name);
System.out.println("Our superclass is: " + food1.getClass().getName());//modification
food1.servesFood();
food2.servesFood();
}}
另一答案
我测试了它,它按预期工作。
`class FoodFactory extends Food {
public Food getFood(String string) {
if (string.equals("Fruit")) {
return new Fruit("Fruit");
} else {
return new FastFood("FastFood");
}
}
}
class Fruit extends Food {
public Fruit(String name1) {
super.name = name1;
}
}
class FastFood extends Food {
public FastFood(String name1) {
super.name = name1;
}
}
class Food {
public String name = null;
public Food() {
}
public Food(String string) {
this.name = string;
}
public void servesFood() {
System.out.println("I'm serving " + this.name);
}
}
class Solution1 {
public static void main(String[] args) throws java.lang.Exception {
FoodFactory myFoods = new FoodFactory();
Food food1 = myFoods.getFood("FastFood");
Food food2 = myFoods.getFood("Fruit");
System.out.println("My name is: " + food1.getClass().getName());
System.out.println("My name is: " + food2.getClass().getName());
System.out.println("Our superclass is: "
+ food1.getClass().getSuperclass().getName());// modification
food1.servesFood();
food2.servesFood();
}
}`
另一答案
类似于给定的答案,但与抽象的食品类。
class FoodFactory extends Food {
public Food getFood(String string) {
if (string.equals("Fruit")) {
return new Fruit();
} else if (string.equals("FastFood"){
return new FastFood();
}
return null;
}
@Override
public void servesFood() {
}
}
class Fruit extends Food {
@Override
public void servesFood() {
System.out.println("I'm serving " + getClass().getName());
}
}
class FastFood extends Food {
@Override
public void servesFood() {
System.out.println("I'm serving " + getClass().getName());
}
}
abstract class Food {
public abstract void servesFood();
}
class Solution1 {
public static void main(String[] args) throws java.lang.Exception {
FoodFactory myFoods = new FoodFactory();
Food food1 = myFoods.getFood("FastFood");
Food food2 = myFoods.getFood("Fruit");
System.out.println("My name is: " + food1.getClass().getName());
System.out.println("My name is: " + food2.getClass().getName());
System.out.println("Our superclass is: "
+ food1.getClass().getSuperclass().getName());// modification
food1.servesFood();
food2.servesFood();
}
}
另一答案
我检查了答案,但是我做了这个结果,注意目标是带有所有参数的复合短语。在许多情况下,答案必然只是在标记的摘录之后,并且从不使用如下所述的整个课程。
编译并取得成功!
/*
* My name is: Fastfood
* My name is: Fruits
* Our superclass is: Food
* I'm serving Fastfood
* I'm serving Fruits
*/
class Solution {
public static void main(String[] argh) {
foodFactory myFoods = new foodFactory();
Food food1 = myFoods.getFood("Fastfood");
Food food2 = myFoods.getFood("Fruits");
System.out.println("My name is: " + food1.getClass().getName());
System.out.println("My name is: " + food2.getClass().getName());
System.out.println("Our superclass is: " + food1.getClass().getSuperclass().getName());
food1.serveFood();
food2.serveFood();
}
}
// attention: from here you will have the correct use
class foodFactory extends Solution {
public Food getFood(String food) {
if (food.equals("Fastfood")) {
return new Fastfood();
} else {
return new Fruits();
}
}
}
class Food extends Solution {
public void serveFood() {
System.out.println("I'm serving " + this.getClass().getName());
}
}
class Fruits extends Food {
}
class Fastfood extends Food {
}
以上是关于我对java继承有一个非常基本的了解。有人可以解释一下应该怎么做?的主要内容,如果未能解决你的问题,请参考以下文章