public class Person
{
String name;
int age;
Person()
{
}
Person(String name,int age)
{
this.name=name;
this.age=age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public void showMsg()
{
System.out.println("姓名:"+name+",年齡"+age);
}
}
public class Teacher1 extends Person
{
String xk;//學(xué)科
Teacher1() //無(wú)參構(gòu)造方法
{
}
Teacher1(String xk) //有參構(gòu)造方法
{
this.xk=xk;
}
public String getXk()
{
return xk;
}
public void setXk(String xk)
{
this.xk = xk;
}
public void jk() //講課方法
{
System.out.println(name+"老師,講授"+xk+"課");
}
}
public class Student1 extends Person
{
int score;//分?jǐn)?shù)
Student1()
{
}
Student1(int score)
{
this.score=score;
}
public int getScore()
{
return score;
}
public void setScore(int score)
{
this.score = score;
}
public void ks() //考試方法
{
System.out.println(name+"同學(xué),考試得了"+score+"分");
}
}
public class TestTeacherStudent
{
public static void main(String[] args)
{
Teacher1 teacher1 = new Teacher1();
teacher1.name="王小平";
teacher1.xk="Java";
teacher1.jk();
Student1 student1 = new Student1();
student1.name="李小樂(lè)";
student1.score=90;
student1.ks();
}
}
批注 2020-07-22 141300.png
97F7A451-5BFE-4b64-9D19-E14875A73ED8.png
public class Auto
{
private String brands;//品牌
private double length; //車長(zhǎng)
private double price; //價(jià)格
public Auto()
{
}
public Auto(String brands,double length,double price)
{
this.brands=brands;
this.length=length;
this.price=price;
}
public String getBrands()
{
return brands;
}
public void setBrands(String brands)
{
this.brands = brands;
}
public double getLength()
{
return length;
}
public void setLength(double length)
{
this.length = length;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
public void showcar()
{
System.out.println("車型:"+brands);
System.out.println("\t價(jià)格:"+price);
System.out.println("\t車長(zhǎng):"+length);
}
}
public class SUV extends Auto
{
private int miniLength=4295;
private int midLength=5070;
public SUV(double length,double price)
{
super("SUV",length,price);
}
public boolean smallSUV()
{
return getLength()<=miniLength;
}
public boolean largeSUV()
{
return getLength()>midLength;
}
public boolean midSUV()
{
return getLength()>miniLength&&getLength()<=midLength;
}
}
import java.util.ArrayList;
public class TestAutoSUV
{
public static void main(String[] args)
{
// SUV[] suv=new SUV[3];
SUV suv1 = new SUV(5079,750000);//創(chuàng)建對(duì)象
SUV suv2 = new SUV(4813,760000);
SUV suv3 = new SUV(4526,770000);
ArrayList<SUV> list=new ArrayList<>();//對(duì)象添加到集合中
list.add(suv1);
list.add(suv2);
list.add(suv3); //遍歷集合宰翅,查詢中型SUV
for (int i = 0; i <list.size(); i++) {
SUV suv=list.get(i);
if(suv.midSUV())
{
suv.showcar();
}
}
}
}