目录
一,面向对象基础训练任务
1,
编写 Car 类,属性有品牌(brand)和颜色(color),show 方法打印 所有属性。
代码
package com.kaikeba.demo;
import java.util.Scanner;
/*
* 编写 Car 类,属性有品牌(brand)和颜色(color),show 方法打印 所有属性。
*/
public class Demo1 {
public static void main(String[] args) {
// 类Car的测试代码
System.out.println("全参构造方法的对象");
new Car("宝马", "白色").show(); // 匿名对象的使用(只用一次故采用匿名对象)
System.out.println();
System.out.println("无参构造方法的对象");
new Car().show();
}
}
// Car类
class Car{
// 成员变量
String brand;
String color;
// 方法
Car(){ // 无参构造方法
brand = "空";
color = "空";
}
Car(String brand1, String color1){ // 全参构造方法
brand = brand1;
color = color1;
}
void show() { // 函数
System.out.println("车的商标为:" + brand);
System.out.println("车的颜色为:" + color);
}
}
2,
定义一个游戏类,包括游戏的属性包括:游戏名、类型、大小、星级、 介绍等,可以调用方法输出游戏的介绍
代码
package com.kaikeba.demo;
public class Demo2 {
public static void main(String[] args) {
Game game1 = new Game(); // 无参构造方法
game1.name = "王者荣耀";
game1.type = "休闲、竞技";
game1.size = 2000;
game1.star = 3.1f;
game1.info = "《王者荣耀》是腾讯第一5V5团队公平竞技手游,国民MOBA手游大作! 5V5王者峡谷、公平对战,还原" +
"MOBA经典体验;契约之战、五军对决、边境突围等,带来花式作战乐趣! 10秒实时跨区匹配,与好友开黑上分," +
"向最强王者进击!多款英雄任凭选择,一血、五杀、超神,实力碾压,收割全场!敌军即将到达战场,王者召唤师快来集结好友,准备团战,就在《王者荣耀》!";
game1.showInfo();
}
}
class Game{
// 属性
String name; // 游戏名称
String type; // 游戏类型
int size; // 游戏大小 单位为M
float star; // 游戏星级 最大值为5
String info; // 游戏介绍
// 方法
Game(){ // 无参构造方法
name = "空";
type = "空";
size = 0;
star = 0.0f;
info = "无";
}
Game(String name1, String type1, int size1, int star1, String info1){ // 全参构造方法
name = name1;
type = type1;
size = size1;
star = star1;
info = info1;
}
void showInfo() { // 输出游戏介绍
System.out.println("游戏名称为:" + name);
System.out.println("游戏类型为:" + type);
System.out.println("游戏大小为:" + size + "M");
System.out.println("游戏星级为:" + star);
System.out.println("游戏介绍:" + info);
}
}
3,
定义并测试一个代表员工的 Employee 类。它的属性包括“员工姓名”、 “员工号码”、“员工基本薪水”、“员工薪水增长率”;他的方法包括“构 造方法”、“获取员工姓名”、“获取员工号码”、“获取员工基本薪水”、 “计算薪水增长额”及“计算增长后的工资总额”。
代码
package com.kaikeba.demo;
public class Demo3 {
public static void main(String[] args) {
Employee person1 = new Employee("张三", "666666", 20000, 0.1f);
// 获得员工姓名
String name = person1.getName();
System.out.println("员工姓名为:" + name);
// 获得员工ID
String ID = person1.getID();
System.out.println("员工ID为:" + ID);
// 获得员工基本薪水
float baseSalary = person1.getBaseSalary();
System.out.println("员工基本薪水为:" + baseSalary + "元");
// 获得员工薪水增长额
float increaseSalary = person1.getSalaryIncrease();
System.out.println("员工薪水增长额为:" + increaseSalary + "元");
// 获得增长后的工资总额
float finalSalary = person1.getFinalSalary();
System.out.println("员工增长后的工资总额为:" + finalSalary + "元");
}
}
class Employee{
/*
* 属性
*/
String name;
String ID;
float baseSalary;
float salaryGrowthRate;
/*
* 方法
*/
// 无参构造方法
Employee(){
name = "空";
ID = "000000";
baseSalary = 0.0f;
salaryGrowthRate = 0.0f;
}
// 全参构造方法
Employee(String name1, String ID1, float baseSalary1, float salaryGrowthRate1){
name = name1;
ID = ID1;
baseSalary = baseSalary1;
salaryGrowthRate = salaryGrowthRate1;
}
// 获取员工姓名
String getName() {
return name;
}
// 获取员工号码
String getID() {
return ID;
}
// 获取员工基本薪资
float getBaseSalary() {
return baseSalary;
}
// 获取薪水增长额
float getSalaryIncrease() {
return baseSalary * salaryGrowthRate;
}
// 获取增长后的薪水
float getFinalSalary() {
return baseSalary * salaryGrowthRate + baseSalary;
}
}