大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
package com.Painter.Demo1;
成都创新互联公司专注于高安企业网站建设,响应式网站,商城网站开发。高安网站建设公司,为高安等地区提供建站服务。全流程定制开发,专业设计,全程项目跟踪,成都创新互联公司专业和态度为您提供的服务
public class AbstractDemo {
// 用java 语言编程实现定义抽象水果类,定义其子类实现其抽象的方法。
public static void main(String[] args)
{
Fruits apple = new Apple();
Fruits orange = new Orange();
apple.getFruits();
orange.getFruits();
}
}
abstract class Fruits
{
public abstract void getFruits();
}
class Apple extends Fruits
{
public void getFruits()
{
System.out.println("苹果");
}
}
class Orange extends Fruits
{
public void getFruits()
{
System.out.println("橘子");
}
}
/**
* 1.定义一个水果类Fruit,符合如下要求:
(a) 类Fruit的成员变量;
weight表示水果的质量,数据类型为float
color表示水果的颜色,数据类型为string.
(b)类Fruit的成员方法:
getWeight()获得水果的质量
getColor()获得水果的颜色
2、按照第1题中的水果类Fruit的定义,创建该类的对象f1和f2,其中f1的weight值为0.86, color值为yellow;
f2的weight值为0.73, color值为red; 存储并输出f1和f2的值,计算f1和f2的平均值。
* @author Administrator
*
*/
public class Fruit {
private float weight;
private String color;
//构造函数:用于存储对象的2个值
public Fruit(float weight, String color){
this.weight = weight;
this.color = color;
}
public float getWeight() {
return this.weight;
}
public String getColor() {
return this.color;
}
//求重量的平均值
public static float avg(float w1, float w2){
return w1*w2/2;
}
public static void main(String[] args){
Fruit f1 = new Fruit((float) 0.86, "yellow");
Fruit f2 = new Fruit((float) 0.73, "red");
System.out.println("f1:" + "\n" + " weitht:" + f1.getWeight() + "\n" + " color:" + f1.getColor());
System.out.println("f2:" + "\n" + " weitht:" + f2.getWeight() + "\n" + " color:" + f2.getColor());
System.out.println("平均值:" + Fruit.avg(f1.getWeight(), f2.getWeight()));
}
}
这么简单的题目也不会,你学了点啥?
class Fruit
{
public Fruit()
{
System.out.println("Fruit类的构造方法。");
}
}
class Apple extends Fruit
{
public Apple()
{
super();
System.out.println("Apple类的构造方法。");
}
}
弄了一下,代码如下,你可以参考参考:
Fruit类:
public abstract class Fruit {
private String shape;
public String getShape() {
return shape;
}
public void setShape(String shape) {
this.shape = shape;
}
public abstract void eat();
}
Apple类:
public class Apple extends Fruit {
public Apple(String shape) {
setFatherShape(shape);
}
public void setFatherShape(String shape)
{
super.setShape(shape);
}
public String getFatherShape()
{
return super.getShape();
}
@Override
public void eat()
{
System.out.println(getFatherShape()+"的苹果好甜");
}
}
Banana类:
public class Banana extends Fruit {
public Banana(String shape) {
setFatherShape(shape);
}
public void setFatherShape(String shape)
{
super.setShape(shape);
}
public String getFatherShape()
{
return super.getShape();
}
@Override
public void eat()
{
System.out.println(getFatherShape()+"的香蕉好香");
}
}
Orange类:
public class Orange extends Fruit {
public Orange(String shape) {
setFatherShape(shape);
}
public void setFatherShape(String shape)
{
super.setShape(shape);
}
public String getFatherShape()
{
return super.getShape();
}
@Override
public void eat()
{
System.out.println(getFatherShape()+"的桔子好酸");
}
}
Game类:
public class Game {
public Fruit luckDraw(){
Random random=new Random();
int luckNum= random.nextInt(3);//随机产生一个0-2之间的数
Fruit fruit = null;
//0-苹果(圆圆的)、1-香蕉(弯弯的)、2-桔子(长长的)
if(luckNum==0){
fruit=new Apple("圆圆的");
}else if (luckNum==1) {
fruit=new Banana("弯弯的");
}else if (luckNum==2) {
fruit=new Orange("长长的");
}
return fruit;
}
public static void main(String[] args) {
Fruit[] fruits=new Fruit[10];
Game game=new Game();
for (int i=0;i10;i++) {
fruits[i]=game.luckDraw();
fruits[i].eat();
}
}
}
运行结果:
弯弯的的香蕉好香
弯弯的的香蕉好香
长长的的桔子好酸
长长的的桔子好酸
圆圆的的苹果好甜
长长的的桔子好酸
圆圆的的苹果好甜
弯弯的的香蕉好香
长长的的桔子好酸
长长的的桔子好酸
楼主若觉得回答有所帮助,望采纳,谢谢!