public class Good {
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:域名與空間、網(wǎng)頁空間、營銷軟件、網(wǎng)站建設(shè)、市中網(wǎng)站維護(hù)、網(wǎng)站推廣。
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();
}
}
主要思路;
。創(chuàng)建父類子類你自己看著敲進(jìn)去,比較簡單。
每個函數(shù)都要有一個帶參數(shù)的構(gòu)造函數(shù);當(dāng)數(shù)組中的參數(shù)傳入時可以對子類對象初始化。
最后比較排序我是用另一個數(shù)組接收原數(shù)組元素(對象)中的getWeight() 方法的返回值用于排序,然后在找到對應(yīng)的值打出來就能達(dá)到你要的效果了。
父類:
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;
}
}
}
}
}
最后面這個是主函數(shù)的類。。請他三個子類在中間,有不明白的地方在追問。
public class WuMingFen {
private String theMa;// 面碼
private int quantity;// 粉的分量(兩)
private boolean likeSoup;// 是否帶湯
// 構(gòu)造出來的粉對象是酸辣面碼、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;// 給個默認(rèn)
}
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();
}
}