task4学了类与方法、继承与多态方面的知识,这里写一下练习题的代码,有问题请指教。
练习1
设计加油站类和汽车类,加油站提供一个给车加油的方法,参数为剩余汽油数量。每次执行加油方法,汽车的剩余汽油数量都会加 2。
public class test1 {
public static void main(String[] args)
{
test1 station = new test1();
int[] a= new int[]{1,2,3};
System.out.println("加油次数 剩余汽油数量");
for (int i = 0;i <a.length;i++)
{
int c;
c=station.chang(a[i]);
System.out.println("加油"+(i+1)+"次 "+"剩余汽油数量:"+c);
}
} public int chang(int b)
{
b =b*2;
return b;
}
}
输出结果:
练习2
智能手机的默认语言为英文,但制造手机时可以将默认语言设置为中文。编写手机类, 无,参构造方法使用默认语言设计,利用有参构造方法修改手机的默认语言。
public class test2 { // 创建手机类
String language; // 手机语言
// 有参数构造方法,参数是手机语言
public test2(String language) { // 参数为手机语言的构造方法
this.language = language; // 将参数language的值付给属性language
System.out.println("目前手机的语言为"+language);
}
// 无参数构造方法,默认是英语
public test2() { // 默认构造方法
// 调用参数为手机语言的构造方法,并设置手机语言为英语
this("English");
}
public static void main(String[] args) {
test2 language1 = new test2(); // 创建无参的手机语言对象
test2 language2 = new test2("Chinese"); // 创建手机语言对象,且修改为中文
}
}
练习3
张三去KFC买可乐,商家默认不加冰块。但是张三可以要求加 3 个冰块。请利用有参构造方法实现上述功能。
public class test3 { // 创建类
int ice; // 冰块数量
// 有参数构造方法,参数是手机语言
public test3(int ice) { // 参数为冰块数量的构造方法
this.ice = ice; // 将参数ice的值付给属性ice
System.out.println("加了"+ice+"个冰块");
}
public static void main(String[] args) {
test3 ice1 = new test3(3); // 创建冰块对象,且设置为3
}
}
练习4
创建教师类,类中有姓名、性别和年龄三个属性,在构造方法中使用 this 关键字分别为这三个成员属性赋值。
public class test4
{
String name;
String sex;
String old;
public test4(String name,String sex,String old)
{
this.name = name;
this.sex = sex;
this.old = old;
}
public void broow()
{
System.out.println("老师的个人信息是:"+"姓名:"+name+"\t性别:"+sex+"\t年龄:"+old);
}
public static void main(String[] args)
{
test4 a = new test4("李华","男","49");
a.broow();
}
}
输出结果:
练习5
一只大熊猫,长 1.3 米,重 90千克。在自定义方法中使用this关键字调用类的成员变量并在控制台输出这只大熊猫的信息。
public class test5 {
double length=1.3;
double weight=90;
public String Panda(){
return "熊猫体长:"+this.length+"m\n熊猫体重:"+this.weight+"kg";
}
public static void main(String[] args){
test5 panda = new test5();
System.out.println(panda.Panda());
}
}
输出结果:
练习6
创建信用卡类,有两个成员变量分别是卡号和密码,如果用户开户时没有设置初始密码,则使用"123456"作为默认密码。设计两个不同的构造方法,分别用于用户设置密码和用户未设置密码两种构造场景。
import java.util.Scanner;
public class test6 {
String number;
String password;
public void account(String number,String password){
this.number=number;
this.password=password;
System.out.println("您的账号:"+number+"\n密码:"+password);
}
public void account_n(String number){
this.number=number;
System.out.println("您的账号:"+number+"\n密码:123456");
}
public static void main(String[] args){
String nu;
String pa;
System.out.println("请输入账号:");
Scanner n=new Scanner(System.in);
nu=n.next();
System.out.println("请输入密码:");
Scanner p=new Scanner(System.in);
pa=p.nextLine();
if(pa.equals("")){//如果不输入密码直接回车则设置为默认密码
test6 a=new test6();
a.account_n(nu);
}else{
test6 b=new test6();
b.account(nu,pa);
}
}
}
输出结果:
练习7
创建银行卡类,并设计银行卡的两个子类∶ 储蓄卡与信用卡。
class BankCard {
String id="123456789";
}
class DepositCard extends BankCard {
public static void main(String[] args) {
BankCard bc = new BankCard();
System.out.println("储蓄卡,卡号:" + bc.id);
}
}
class CreditCard extends BankCard {
public static void main(String[] args) {
BankCard bc = new BankCard();
System.out.println("信用卡,卡号:" + bc.id);
}}
练习8
设计火车类和高铁类,高铁类继承火车类,不管火车类的行进速度是多少,高铁的行进速度永远是火车的二倍。
public class Train {
double speed;
}
class Fast_train extends Train{
public static void main(String[] args){
double fast_speed;
Train t = new Train();
fast_speed = t.speed*2;
}
}
练习9
设计人类,有二个自我介绍的方法,输出"我是 XXX";设计博士类,继承人类,博士类自我介绍时输出"我是XXX 博士"。
public class People {
String sayHello(){
return "我是XXX";
}
}
class Doctor extends People{
String sayHello(){
return super.sayHello()+" 博士";
}
public static void main(String[] args){
People p = new People();
System.out.println(p.sayHello());
Doctor d = new Doctor();
System.out.println(d.sayHello());
}
}
练习10
使用方法的重载描述所有的超市都支持现金付款,但大型商超还支持刷卡付款。
public class OverLoad
{
public static String connect(String a)
{
return a;
}
public static String connect(String a,String b)
{
return a + b;
}
public static void main(String args[])
{
System.out.println(connect("所有的超市都支持现金付款"));
System.out.println(connect("所有的超市都支持现金付款",",但大型商超还支持刷卡付款"));
}
}
练习11
使用方法的重载描述所有的汽车都至少有两块脚踏板,但手动挡汽车有三块脚踏板。
public class OverLoad1
{
public static String connect(String a)
{
return a;
}
public static String connect(String a,String b)
{
return a + b;
}
public static void main(String args[])
{
System.out.println(connect("所有的汽车都至少有两块脚踏板"));
System.out.println(connect("所有的汽车都至少有两块脚踏板",",但手动挡汽车有三块脚踏板"));
}
}
参考资料:
【1】Datawhale开源资料_类与方法
【2】Datawhale开源资料_继承与多态