
正文
java继承的代码题 java继承写法
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
请教两道JAVA的编程题目。关于继承与重载,请按下列要求编写代码。
public class Good {
String goodname;
double goodprice;
boolean isMem;
public Good(String name,double price){
this.goodname=name;
this.goodprice=price;
this.isMem=false;
}
public void setName(String name){
this.goodname=name;
}
public void setPrice(double price){
this.goodprice=price;
}
public void setMem(){
this.isMem=!this.isMem;
}
public void discount(){
System.out.println(this.goodprice);
}
}
public class Milk extends Good {
double memprice;
public Milk(String name, double price,double memprice) {
super(name, price);
this.memprice=memprice;
// TODO Auto-generated constructor stub
}
public void setMemPrice(double memprice){
this.memprice=memprice;
}
public void discount(){
if(isMem)
System.out.println(this.memprice*0.8);
else
System.out.println(this.goodprice*0.8);
}
public static void main(String args[]){
Milk test=new Milk("牛奶",3,2.6);
test.discount();
test.setMem();
test.discount();
}
}
相关问答
Q1: 简单的java 编程题 关于继承
package javaapplication4;
public class Rect {
protected int length;/////这个地方不能变成私有属性java继承的代码题,因为后面继承的类也需要继承它。
protected int width;
public Rect(int length,int width)
{
this.length=length;
this.width=width;
}
public void setLength(int length)
{this.length=length;br }
public void setWidth(int width)
{this.width=width;br }
public int getLength()
{return length;br }
public int getWidth()
{return width;br }
public int getArea()
{return length*width;br }
public int getCycle()
{return (length+width)*2;br }}
////////////////////////////////////////////////////////////////////////////////////////////////////////
package javaapplication4;
public class PlainRect extends Rect
{///此类并未继承父类的长宽属性,所以父类的设计是不合理的。应将其属性改为protected.
protected int startX,startY;
public PlainRect(int length,int width,int startx,int starty)
{
super(length,width);
startX=startx;
startY=starty;
}
public PlainRect()
{
super(0,0);
startX=0;
startY=0;
}
public boolean isInside(double x,double y)
{
boolean b=false;
if(xstartXxstartX+length)
if(ystartYystartY+width)
b=true;
return b; }}
////////////////////////////////////////////////////////////////////////////////////////////////////////
package javaapplication4;
public class Main {
public static void main(String[] args) {
PlainRect Pr1=new PlainRect(20,10,10,10);
System.out.println("面积为java继承的代码题:"+Pr1.getArea());
System.out.println("周长为java继承的代码题:"+Pr1.getCycle());
System.out.println("点(25.5,13)"+(Pr1.isInside(25.5, 13)?"在":"不在")+"此方形内");
} }
Q2: JAVA:编写一个动物的继承关系代码.
下面是一个简单的 Java 程序示例,用于实现山羊和狼的继承关系,并在测试类中进行验证:
Animal.java:
```java
public abstract class Animal {
public void walk() {
System.out.println("走路");
}
public abstract void eat();
}
```
Goat.java:
```java
public class Goat extends Animal {
@Override
public void eat() {
System.out.println("吃草");
}
}
```
Wolf.java:
```java
public class Wolf extends Animal {
@Override
public void eat() {
System.out.println("吃肉");
}
}
```
AnimalTest.java:
```java
public class AnimalTest {
public static void main(String[] args) {
Goat goat = new Goat();
wolf wolf = new wolf();
goat.eat(); // 输出:吃草
wolf.eat(); // 输出:吃肉
goat.walk(); // 输出:走路
wolf.walk(); // 输出:走路
}
}
```
在此程序中,我们定义了一个 `Animal` 类作为基类,包含了所有动物都会有的行为(例如走路),并使用抽象方法 `eat()` 表示不同动物的饮食习惯。然后通过继承实现 `Goat` 和 `Wolf` 两个子类,并分别重写 `eat()` 方法。
在测试类中,我们分别创建了一个 `Goat` 和一个 `Wolf` 对象,并分别调用其 `eat()` 和 `walk()` 方法进行验证。可以看到,不同的动物具备不同的饮食习惯,但它们的走路行为是一致的。
Q3: JAVA继承问题 求代码
第一个:
public class Yaojing {
protected String name;
protected int age;
protected String gender;
public void showBasicInfo() {
System.out.println(toString());
}
public void eatTangSeng() {
System.out.println("吃饱了");
}
@Override
public String toString() {
return "Yaojing [name=" + name + ", age=" + age + ", gender=" + gender + "]";
}
}
第二个类
public class Zhizhujing extends Yaojing {
public void buildNet(){
System.out.println("蜘蛛在织网");
}
}
第三个类
public class Baigujing extends Yaojing {
public void beBeauty(){
System.out.println("白骨精");
}
}
Q4: 求编写一个Java类的继承程序?
java基础,继承类题目:编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类 E
21.编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类
E。要求:
(1) Monkey类中有个构造方法:Monkey (String s),并且有个public void speak()
方法,在speak方法中输出“咿咿呀呀......”的信息。
(2)People类是Monkey类的子类,在People类中重写方法speak(),在speak方法
中输出“小样的,不错嘛!会说话了!”的信息。
(3)在People类中新增方法void think(),在think方法中输出“别说话!认真思考!”
的信息。
(4)在主类E的main方法中创建Monkey与People类的对象类测试这2个类的功
能。、
复制代码
package zhongqiuzuoye;
public class Monkey {
Monkey(String s) //构造
{}
public void speak()
{
System.out.println("咿咿呀呀......");
}
}
Q5: java 继承题目
//继承
public class test {
public static void main (String args[]){
Manager m=new Manager ("小明",5000,"北京","java工程师"); //创建一个经理对象
System.out.println("经理的信息:");
m.show(); //输出信息
Director d =new Director ("小娃",6000,"广州",500); //创建一个director对象
System.out.println("\n Director 的信息:"); //调用函数输出信息
d.show();
}
}
class Employee{ //创建Employee类
String name ;
float basic;
String address;
Employee (String nam,float bas,String add){ //初始化
name=nam;
basic=bas;
address=add;
}
public void show (){
System.out.println("名字:"+this.name);
System.out.println("基本工资::"+this.basic);
System.out.println("地址:"+this.address);
}
}
class Manager extends Employee{ //Manager类继承了父类的属性和方法
String department;
Manager (String nam, float bas, String add,String dep) {
super(nam, bas, add); //使用super关键字
department=dep;
}
public void show() {
super.show();
System.out.println("部门:"+department);
}
}
class Director extends Employee{ //同上一个类一样的
double transportAllowance;
Director(String nam, float bas, String add,double tra) {
super(nam, bas, add);
transportAllowance=tra;
}
public void show() {
super.show();
System.out.println("交通补贴:"+transportAllowance);
}
}
关于java继承的代码题和java继承写法的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







