class Employee {
static { // 随着的类的加载而执行, 且执行一次, 由JVM自动执行, 相当于类构造器
System.out.println("static{}...静态语句块");
//return;
company = "BAIDU";
}
private static String company = "baidu"; // 静态属性, 类变量, 它的存在不需要对象,只需要类即可
// 属性的声明语句和赋值语句是分别执行的, 先执行声明,开辟空间并写0, 后执行赋值操作, 赋值操作和静态代码块同级别
static {
System.out.println("static{}2...");
}
public static void setCompany(String company) {
Employee.company = company;
}
public static String getCompany() {
return company;
}
public static void test() {// 静态方法 类方法, 它的调用不需要对象,只需要通过类即可调用
System.out.println("static test()...");
// 在静态环境中this关键字被禁止
// 也不能直接访问非静态成员, 因为非静态成员的存在依赖对象!!
// 如果能过对象就可以访问了
/*
name = "李四";
age = 40;
salary = 4000;
*/
Employee emp = new Employee();
System.out.println(emp);
emp.name = "李四"; // 只有在同一个类中才可以直接访问私有属性
emp.age = 40;
emp.salary = 4000;
System.out.println(emp);
}
private static int count = 0;
{ // 动态语句块, 在创建对象时无条件执行一次!, 先于构造器执行
System.out.println("动态语句块2");
name = "xxx";
}
private final int ID; // 空final量, 危险!!! 必须保证对其进行一次赋值!!
private String name = "XXX";
private int age;
private double salary;
{ // 动态语句块, 在创建对象时无条件执行一次!, 先于构造器执行
System.out.println("动态语句块1");
this.ID = ++count; // 非静态环境中直接访问静态成员!!, 进行一次赋值
}
public Employee() {
}
public Employee(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
System.out.println("Employee()...");
}
public Employee(String name, int age) {
}
/*
public void setID(int ID) {
this.ID = ID;
}*/
public int getID() {
return ID;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getSalary() {
return salary;
}
@Override
public String toString() {
return "ID:" + ID + ",姓名:" + name + ",年龄:" + age + ",工资:" + salary;
}
}
class EmployeeTest2 {
public static void main(String[] args) {
Employee.test(); // 类在第一次使用时加载, 调用test方法, 创建对象
}
}
class EmployeeTest {
public static void main(String[] args) {
//System.out.println(Employee.company);
System.out.println(Employee.getCompany());
//Employee.company = "百度";
Employee.setCompany("百度");
//System.out.println(Employee.company);
System.out.println(Employee.getCompany());
Employee.test();
Employee emp1 = new Employee("张三", 30, 3000);
Employee emp2 = new Employee("张四", 40, 4000);
Employee emp3 = new Employee("张五", 50, 5000);
Employee emp4 = new Employee("张六", 60, 6000);
System.out.println(emp1);
System.out.println(emp2);
System.out.println(emp3);
System.out.println(emp4);
Employee emp5 = new Employee();
Employee emp6 = new Employee();
System.out.println(emp5);
System.out.println(emp6);
}
}
class EmployeeSub extends Employee {}
class EmployeeSubTest {
public static void main(String[] args) {
System.out.println(EmployeeSub.getCompany());
}
}
/*编写上衣Frock类,不继承任何父类。包含尺寸size、颜色color、价格price等属性 构造器,get/set
在上衣类Frock中覆盖toString方法,用来返回 frock的详细信息。
在Frock类中声明私有的静态属性currentNum,初始值为100000,作为衣服出厂的序列号起始值。
声明公有的静态方法getNextNum,作为生成上衣唯一序列号的方法。每调用一次,将currentNum增加100,并作为返回值。
*/
/*
在Frock类中声明serialNumber属性,并提供对应的get方法;
在Frock类的构造器中,通过调用getNextNum方法为Frock对象获取唯一序列号;
在FrockTest 类的main方法中,分别创建三个Frock 对象,并打印三个对象的序列号,验证是否为按100递增。
*/
/*
在Frock类中声明公有静态常量INCREMENT,值为100,修改静态方法getNextNum代码,用INCREMENT常量代替原有的增量值100。
执行FrockTest类的main方法,验证修改是否正确。
将Frock类中的serialNumber属性改为空final变量,并且仍然在构造器中为其赋值;
为serialNumber增加公有的set方法,确认是否能够编译通过?
去掉set方法,执行FrockTest 类的main方法,验证程序是否正确。
*/
public class Frock {
public static final int INCREMENT;
private static int currentNum = 100000;
static {
INCREMENT = 100;
currentNum = 150000;
}
public static int getNextNum() {
return currentNum += INCREMENT;
}
private final int serialNumber;
private int size;
private String color;
private double price;
{
this.serialNumber = getNextNum();
}
public Frock() {
}
public Frock(int size, String color, double price) {
this.size = size;
this.color = color;
this.price = price;
}
public void setSize(int size) {
this.size = size;
}
public int getSize() {
return size;
}
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void setPrice(double price) {
this.price = price;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return "序列号:" + serialNumber + ",大小:" + size + ",颜色:" + color + ",价格:" + price;
}
}
/*
在FrockTest 类的main方法中,分两次调用getNextNum方法,获取序列号并打印输出
*/
class FrockTest {
public static void main(String[] args) {
System.out.println(Frock.getNextNum());
System.out.println(Frock.getNextNum());
Frock f1 = new Frock(180, "白色", 200);
Frock f2 = new Frock(170, "黑色", 100);
Frock f3 = new Frock(185, "红色", 300);
System.out.println(f1);
System.out.println(f2);
System.out.println(f3);
Frock f4 = new Frock();
System.out.println(f4);
}
}
/*
编写一个类实现银行账户的概念,包含的属性有“帐号”、“密码”、“存款余额”、“利率”、“最小余额”,
定义封装这些属性的方法。账号要自动生成。
考虑:哪些属性可以设计成static属性。所有对象是否可以共享
*/
public class Account {
static double interestRate = 0.045;
static double minBalance = 99.9;
static final int COUNT = 10;
static int id = 1000;
public static int getNextID() {//下一个id值被id + COUNT赋值并返回
return id += COUNT;
}
private final int ID;
private int password;
private double balance;
{
ID = getNextID();//常量ID 被静态方法getNextID方法的返回值赋值
}
public Account() {}
public Account(int password,double balance) {
this.password = password;
this.balance = balance;
}
public void setPassword(int password) {
this.password = password;
}
public int getPassword() {
return password;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
@Override
public String toString() {//重写String类的toString方法,返回Account对象的详细信息
return "账号:" + ID + ",密码:" + password + ",存款余额:" + balance +
",利率:" + interestRate + ",最小余额:" + minBalance;
}
}
class AccountTest {
//编写主类,使用银行账户类,输入、输出3个储户的上述信息。
public static void main(String[] args) {
Account acc1 = new Account(1213,5000);
Account acc2 = new Account(2331,6000);
Account acc3 = new Account(1234,8000);
System.out.println(acc1);
System.out.println(acc2);
System.out.println(acc3);
}
}
/*
Device 设备
ID (是常量,并且要求自动生成)
name
factory
price
*/
public class Device {
static int id = 0;
public static int getNextId() {
return ++id;
}
private final int ID;
private String name;
private String factory;
private double price;
{
ID = getNextId();
}
public Device() {}
public Device(String name,String factory,int price) {
this.name = name;
this.factory = factory;
this.price = price;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setFactory(String factory) {
this.factory = factory;
}
public String getFactory() {
return factory;
}
public void setPrice(double price) {
this.price = price;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return "编号:" + ID + ",名字:" + name + ",生产商:" + factory + ",价格:" + price + "元";
}
}
public class InputDevice extends Device {
private int speed;
public InputDevice() {}
public InputDevice(String name,String factory,int price,int speed) {
super(name,factory,price);
this.speed = speed;
}
@Override
public String toString() {
return super.toString() + ",速度:" + speed;
}
}
class Mouse extends InputDevice {
private boolean wirless;
public Mouse() {}
public Mouse(String name,String factory,int price,int speed,boolean wirless) {
super(name,factory,price,speed);
this.wirless = wirless;
}
@Override
public String toString() {
return super.toString() + ",无线鼠标?" + (wirless ? "是" : "否");
}
}
class Keyboard extends InputDevice {
private int keyCount;
public Keyboard() {}
public Keyboard(String name,String factory,int price,int speed,int keyCount) {
super(name,factory,price,speed);
this.keyCount = keyCount;
}
@Override
public String toString() {
return super.toString() + ",键盘按键数:" + keyCount;
}
}
public class OutputDevice extends Device {
private String color;
public OutputDevice() {}
public OutputDevice(String name,String factory,int price,String color) {
super(name,factory,price);
this.color = color;
}
@Override
public String toString() {
return super.toString() + ",颜色:" + color;
}
}
class Monitor extends OutputDevice {
private int size;
public Monitor() {}
public Monitor(String name,String factory,int price,String color,int size) {
super(name,factory,price,color);
this.size = size;
}
@Override
public String toString() {
return super.toString() + ",尺寸:" + size;
}
}
public class DeviceTest {
//写一个测试类
//创建一个数组, 保存2个鼠标,1个键盘,2个显示器
public static void main(String[] args) {
Device[] des = new Device[5];
//public Mouse(String name,String factory,int price,int speed,boolean wirless)
des[0] = new Mouse("普通鼠标","DELL",120,35,false);
des[1] = new Mouse("机械鼠标","雷蛇",200,40,true);
//public Keyboard(String name,String factory,int price,int speed,int keyCount)
des[2] = new Keyboard("键盘","DELL",180,40,85);
//public Monitor(String name,String factory,int price,String color,int size)
des[3] = new Monitor("普通显示屏","DELL",160,"黑色",15);
des[4] = new Monitor("液晶显示屏","惠普",190,"白色",21);
//遍历输出Device对象的详细信息
for (Device de : des) {
System.out.println(de);
}
System.out.println("----------------------------按照价格排序后---------------------");
//冒泡排序des数组
for (int i = 0;i < des.length - 1;i++) {
for (int j = 0;j < des.length - 1 - i;j++) {
if (des[j].getPrice() > des[j + 1].getPrice()) {
Device tmp = des[j];
des[j] = des[j + 1];
des[j + 1] = tmp;
}
}
}
//遍历输出Device对象的详细信息
for (Device de : des) {
System.out.println(de);
}
}
}
java学习之路 之 高级类特性2-关键字static、final、动态语句块结合的练习题
最新推荐文章于 2023-06-03 07:15:00 发布