练习一:
package sonyi;
/*
练习:
编写一个Java程序,满足以下要求:
1、编写一个Car类,具有:
属性:品牌(mark)----String类型
功能:驾驶(void drive())
2、定义Car类的子类SubCar,具有:
属性:价格(price)、速度(speed)----int型
功能:变速(void speedChange(int speed)),把新速度赋值给speed
3、定义主类,在main方法中创建SubCar类的两个对象,bmw和benchi的对象并测试其对象的特性。
*/
public class CarExercise {
public static void main(String[] args) {
//实现两个对象
SubCar bmw = new SubCar("bmw",100,80);
SubCar benz = new SubCar("benz",110,90);
System.out.println(bmw);
bmw.drive();
bmw.speedChange(200);
System.out.println(benz);
benz.drive();
benz.speedChange(250);
}
}
//子类继承父类
class SubCar extends Car{
private int price;
private int speed;
SubCar(){
}
SubCar(String mark,int price,int speed){
super(mark);
this.price = price;
this.speed = speed;
}
public void speedChange(int speed){
System.out.println(getMark() + "现在变速为:" + speed);
setSpeed(speed);
}
@Override
public String toString() {
return "我是" + getMark() + ",我的价格是:" + price + ", 我现在的速度是:" + speed;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}
//父类
class Car{
private String mark;
Car(){
}
Car(String mark){
this.mark = mark;
}
public void drive(){
System.out.println(getMark() + "正在行驶。。。。");
}
public String getMark() {
return mark;
}
public void setMark(String mark) {
this.mark = mark;
}
}
练习二:
package sonyi;
/*
练习:
编写一个类Calculate1,实现加、减两种运算,然后,编写另一个派生类Calculate2,实现乘、除两种运算。
*/
public class Calculate {
public static void main(String[] args) {
// TODO Auto-generated method stub
Calculate1 c1 = new Calculate1();
Calculate2 c2 = new Calculate2();
System.out.println("c1的加法:" + c1.plus(5, 3));
System.out.println("c1的减法:" + c1.subtraction(5, 3));
System.out.println("c2的加法:" + c2.plus(5, 3));
System.out.println("c2的减法:" + c2.subtraction(5, 3));
System.out.println("c2的乘法:" + c2.multiply(5, 3));
System.out.println("c2的除法:" + c2.remove(5, 3));
}
}
class Calculate1{
public double plus(double x,double y){
return x + y;
}
public double subtraction(double x,double y){
return x - y;
}
}
class Calculate2 extends Calculate1{
public double multiply(double x, double y){
return x*y;
}
public double remove(double x,double y){
return x/y;
}
}