话不多说,直接上题目:
一、编写 Car 类,属性有品牌(brand)和颜色(color),show 方法打印 所有属性。
代码如下:
public static void main(String[] args) {
Car c = new Car();
c.brand = "法拉利";
c.color = "酒红色";
//调用show方法,打印
c.show();
}
}
class Car {
//属性:颜色,品牌
String brand, color;
//定义show方法打印所有属性
public void show() {
System.out.println("颜色:" + brand);
System.out.println("品牌:" + color);
}
效果图:
二、定义一个游戏类,包括游戏的属性包括:游戏名、类型、大小、星级、 介绍等,可以调用方法输出游戏的介绍。
代码如下:
public class demo1 {
public static void main(String[] args) {
Game g = new Game("使命召唤");
g.b = "动作射击类";
g.c = "8G";
g.d = "3A";
g.e = "是一个围绕现代反恐的剧情战争类游戏";
//调用show方法,打印
g.show();
}
}
class Game {
//属性:名称,类型,大小,星级,简介
String a, b, c, d, e;
//游戏名称构造方法
public Game(String n) {
a = n;
}
//定义show方法打印所有属性
public void show() {
System.out.println("名称:" + a);
System.out.println("类型:" + b);
System.out.println("大小:" + c);
System.out.println("星级:" + d);
System.out.println("简介:" + e);
}
}
效果图:
三、定义并测试一个代表员工的 Employee 类。它的属性包括“员工姓名”、 “员工号码”、“员工基本薪水”、“员工薪水增长率”;他的方法包括“构 造方法”、“获取员工姓名”、“获取员工号码”、“获取员工基本薪水”、 “计算薪水增长额”及“计算增长后的工资总额”。
代码如下:
public static void main(String[] args) {
//创建Employee对象
Employee e = new Employee();
//使用set方法设置属性
e.setA("菜月昴"); //员工姓名
e.setB(486); //编号
e.setC(10000); //基本工资
e.setD(0.4f); //增长率
//打印信息 使用get方法获取属性值
System.out.println("员工姓名:" + e.getA());
System.out.println("员工编号:" + e.getB());
System.out.println("员工基本薪水:" + e.getC());
System.out.println("员工薪水增长率:" + e.getD());
System.out.println("薪水增长额:" + e.f()); //薪水增长额度
System.out.println("增长后的工资总额:" + e.g());//增长后的工资
}
}
class Employee {
//属性:姓名,编号,基本薪水,薪水增长率
String a;
int b;
float c, d;
//无参数构造方法
public Employee() {
}
//全参数构造方法
public Employee(String a, int b, float c, float d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
/**
* 方法“获取员工姓名”
*/
public String getA() {
return a;
}
/**
* 方法“设置员工姓名”
*/
public void setA(String a) {
this.a = a;
}
/**
* 方法“获取员工号码”
*/
public int getB() {
return b;
}
/**
* 方法“设置员工号码”
*
* @param nums
*/
public void setB(int b) {
this.b = b;
}
/**
* 方法“获取员工基本薪水”
*
* @return
*/
public float getC() {
return c;
}
/**
* 方法“设置员工基本薪水”
*
* @param basicSalary
*/
public void setC(float c) {
this.c = c;
}
/**
* 方法“获取员工薪水增长率”
*
* @return
*/
public float getD() {
return d;
}
/**
* 方法“设置员工薪水增长率”
*
* @param salaryGrowthRate
*/
public void setD(float d) {
this.d = d;
}
/**
* @return float 增长额
* @Description 计算薪水增长额
* @Param []
*/
public float f() {
return c * d;
}
/**
* @return float 工资总额
* @Description 计算增长后的工资总额
* @Param []
*/
public float g() {
return c * (1 + d);
}
注意:
- 增长率一般是小于1的正数,有小数点,在给变量时应该在末尾加f。
- get为“获取”,set为“设置”。
效果图:
四、编写一个类 Book,代表图书。
- 具有属性:名称(title)、页数(pageNum),其中页数不能少于 200 页,否则输出错误信息,并赋予默认值 200。
- 具有方法: 为各属性设置赋值和取值方法。 detail,用来在控制台输出每 本图书的名称和页数
- 编写测试类 BookTest 进行测试:为 Book 对象的属性赋予初始值,并调 用 Book 对象的 detail 方法,看看输出是否正确
代码如下:
public static void main(String[] args) {
Book b1 = new Book();
b1.setTitle("龙族");
b1.setPageNum(321);
b1.say();
b1.setTitle("三体");
b1.setPageNum(123);
b1.say();
}
}
class Book {
private String title; //书名
private int pageNum; //页数
public Book() {
}
;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
if (pageNum < 200) {
System.out.println("你设置的页数小于200,默认设置为200");
this.pageNum = 200;
} else {
this.pageNum = pageNum;
}
}
public void say() {
System.out.println("名称:" + title + "\n页数:" + pageNum);
}
效果图:
五、通过类描述开课吧的 Java 学员。
- 具有属性: 姓名,年龄,性别,爱好,公司(都是:开课吧),学科(都 是:Java 学科)。
- 思考:请结合 static 修饰属性进行更好的类设计。
代码如下:
public static void main(String[] args) {
Student.company = "开课吧";
Student.subject = "Java";
Student s1 = new Student();
s1.setName("菜月昴");
s1.setAge(17);
s1.setGender('男');
s1.setInterest("运动");
s1.say();
System.out.println("====================");
Student s2 = new Student();
s2.setName("蕾姆");
s2.setAge(15);
s2.setGender('女');
s2.setInterest("旅游");
s2.say();
}
}
class Student {
private String name; //名字
private int age; //年龄
private char gender; //性别
private String interest; //爱好
static String company; //公司
static String subject; //学科
Student() {
}
Student(String name, int age, String interest) {
this.name = name;
this.age = age;
this.interest = interest;
}
String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
int getAge() {
return age;
}
void setAge(int age) {
if (age < 0 || age > 150) {
System.out.println("你设置的年龄有误,默认设为0");
this.age = 0;
} else {
this.age = age;
}
}
char getGender() {
return gender;
}
void setGender(char gender) {
if (gender != '男' && gender != '女') {
System.out.println("你输入的性别有误,默认为:'无'");
gender = '无';
} else {
this.gender = gender;
}
}
String getInterest() {
return interest;
}
void setInterest(String interest) {
this.interest = interest;
}
void say() {
System.out.println("姓名:" + this.name);
System.out.println("年龄:" + this.age);
System.out.println("性别:" + this.gender);
System.out.println("爱好:" + this.interest);
System.out.println("公司:" + company);
System.out.println("学科:" + subject);
}
}
效果图:
六、通过类描述衣服, 每个衣服对象创建时需要自动生成一个序号值。
- 要求:每个衣服的序号是不同的, 且是依次递增 1 的。
代码如下:
public static void main(String[] args) {
//创建Clothes对象
Clothes c1 = new Clothes();
c1.setBrand("耐克");
c1.setColor("黑色");
c1.say();
System.out.println("==================");
Clothes c2 = new Clothes();
c2.setBrand("巴拉");
c2.setColor("红色");
c2.say();
}
}
class Clothes {
private String brand; //品牌
private String color; //颜色
//静态属性count,用于记录衣服对象创建的序号值
static int count;
//衣服序号++
Clothes() {
count++;
}
Clothes(String brand, String color) {
this.brand = brand;
this.color = color;
}
String getBrand() {
return brand;
}
void setBrand(String brand) {
this.brand = brand;
}
String getColor() {
return color;
}
void setColor(String color) {
this.color = color;
}
void say() {
System.out.println("品牌:" + brand);
System.out.println("颜色:" + color);
System.out.println("序号:" + count);
}
}
效果图:
总结
- static表示“静态”的意识,可以用来修饰成员变量和成员方法。静态成员在类加载时加载并初始化。
- this可以调用类中的属性,方法以及构造方法,且表示当前对象。
- private定义的变量只能在此对象内部调用,外部程序就无法调用。