189 8069 5689

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();

}

}

一道java题目大神们看看代码该怎么做,还有重载的含义

主要思路;

。创建父类子类你自己看着敲进去,比较简单。

每个函数都要有一个带参数的构造函数;当数组中的参数传入时可以对子类对象初始化。

最后比较排序我是用另一个数组接收原数组元素(对象)中的getWeight() 方法的返回值用于排序,然后在找到对应的值打出来就能达到你要的效果了。

父类:

package playgame;

public abstract class Pet {

private String name;

private int age;

private double weight;

public void getInformation(String name,int age,double weight){

this.name=name;

this.age=age;

this.weight=weight;

}

public double getWeight(){

return weight;

}

public String toString(){

String str="name="+name+",age="+age+",weight"+weight;

return str;

}

public abstract void speak();

public abstract void eat();

}

package playgame;

public class Bird extends Pet{

private String name;

private double weight;

private double flyspeed;

public Bird(String name,double flyspeed,double weight){

this.name=name;

this.flyspeed=flyspeed;

this.weight=weight;

}

public double getWeight(){

return weight;

}

public void speak(){

System.out.println("我是一只小小鸟");

}

public void eat(){

System.out.println("我最爱吃虫子");

}

public String toString(){

String str="name="+name+",flyspeed="+flyspeed+",weight="+weight;

return str;

}

}

package playgame;

public class Cat extends Pet {

private String name;

private String coulour;

private double weight;

public Cat(String name,String coulour,double weight){

this.name=name;

this.coulour=coulour;

this.weight=weight;

}

public double getWeight(){

return weight;

}

public void speak() {

System.out.println("我是一只猫");

}

public void eat() {

System.out.println("我爱吃鱼");

}

public String toString(){

String str="name="+name+",coulour="+coulour+",weight="+weight;

return str;

}

}

package playgame;

public class Dog extends Pet {

private String name;

private double weight;

private String variety;

public Dog(String name,String variety,double weight){

this.name=name;

this.variety=variety;

this.weight=weight;

}

public double getWeight(){

return weight;

}

public void speak() {

System.out.println("我是一只狗");

}

public void eat() {

System.out.println("我爱吃骨头");

}

public String toString(){

String str="name="+name+",weight="+weight+",variety="+variety;

return str;

}

}

package playgame;

import java.util.Arrays;

public class Text1 {

public static void main(String[] args) {

Pet arr[]={new Dog("狗","藏獒",180000.5),new Dog("狗","藏獒",180006.5),

new Cat("猫","红色",18000.5),new Cat("猫","黑色",18000.5),

new Bird("鸟",20.5,180), new Bird("鸟",22.5,187)};

double arr1[]=new double[6];

for(int i=0;i6;i++){

arr1[i]=arr[i].getWeight();

}

Arrays.sort(arr1);

for(int i=0;i6;i++){

for(int j=0;j6;j++){

if(arr1[i]==arr[j].getWeight()){

System.out.println(arr[j].toString()+"\t");

break;

}

}

}

}

}

最后面这个是主函数的类。。请他三个子类在中间,有不明白的地方在追问。

简单的java构造方法与重载 练习

public class WuMingFen {

private String theMa;// 面码

private int quantity;// 粉的分量(两)

private boolean likeSoup;// 是否带汤

// 构造出来的粉对象是酸辣面码、2两、带汤的

public WuMingFen() {

this.theMa = "酸辣";

this.quantity = 2;

this.likeSoup = true;

}

public WuMingFen(String theMa, int quantity) {

this.theMa = theMa;

this.quantity = quantity;

this.likeSoup = true;// 给个默认

}

public WuMingFen(String theMa, int quantity, boolean likeSoup) {

this.theMa = theMa;

this.quantity = quantity;

this.likeSoup = likeSoup;

}

public void check() {

System.out.println("面码:" + this.theMa + "\n分量:" + this.quantity

+ "(两)\n带汤:" + this.likeSoup);

}

public static void main(String[] args) {

WuMingFen f1 = new WuMingFen("牛肉", 3, true);

WuMingFen f2 = new WuMingFen("牛肉", 2);

WuMingFen f3 = new WuMingFen();

f1.check();

f2.check();

f3.check();

}

}


文章标题:java重载的代码练习,java中重载
分享链接:http://jkwzsj.com/article/hdjgcc.html

其他资讯