「水果java抽象方法」水果抽象类
今天给各位分享水果java抽象方法的知识,其中也会对水果抽象类进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、用java 语言编程实现定义抽象水果类,定义其子类实现其抽象的方法
- 2、用java 语言编程实现定义抽象水果类,定义其子类实现其抽象的方法。
- 3、java程序题:定义一个抽象类-水果,其中包括getWeight()方法,编写程序分别创建苹果、
用java 语言编程实现定义抽象水果类,定义其子类实现其抽象的方法
public abstract class Fruit {
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
private String kind;
private float weight;
private float price;
public abstract void tell(float price, float weight);
}
public class Apple extends Fruit {
public Apple() {
this.setKind("苹果");
}
public static void main(String[] args) {
Apple a = new Apple();
a.setPrice(1.02f);
a.setWeight(5.02f);
a.tell(a.getPrice(), a.getWeight());
}
@Override
public void tell(float price, float weight) {
System.out.println("种类:" + this.getKind() + " 重量:" + weight + " 价格:"
+ price);
System.out.println("总价:" + weight * price);
}
}
用java 语言编程实现定义抽象水果类,定义其子类实现其抽象的方法。
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("橘子");
}
}
java程序题:定义一个抽象类-水果,其中包括getWeight()方法,编写程序分别创建苹果、
水果类
abstract public class Fruit {
abstract public double getWeight();
}
苹果类
public class Apple extends Fruit {
private double weight;
public Apple(double weight) {
this.weight = weight;
}
@Override
public double getWeight() {
return weight;
}
}
橘子类
public class Orange extends Fruit {
private double weight;
public Orange(double weight) {
this.weight = weight;
}
@Override
public double getWeight() {
return weight;
}
}
桃子类
public class Peach extends Fruit {
private double weight;
public Peach(double weight) {
this.weight = weight;
}
@Override
public double getWeight() {
return weight;
}
}
主类
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Fruit[] fruits = { new Peach(12), new Apple(2), new Orange(5) };
for (Fruit fruit : fruits) {
System.out.println(fruit.getClass().getName() + "的重量是"
+ fruit.getWeight());
}
}
}
运行结果
Peach的重量是 12.0
Apple的重量是 2.0
Orange的重量是 5.0
关于水果java抽象方法和水果抽象类的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-23,除非注明,否则均为
原创文章,转载请注明出处。