/**
* 测试类中实例变量赋值的先后顺序
*
* 1. 类中实例变量赋值有哪些位置:
* ① 默认初始化 (只能调用一次)
* ② 显式初始化 (只能调用一次)
* ③ 构造器中初始化 (只能调用一次)
* ④ 代码块中赋值
* ⑤ 通过"对象.属性" 或 "对象.方法"的方法给属性赋值(可以多次调用)
*
* 2. 类中实例变量赋值的先后顺序
* ① - ② / ④ - ③ - ⑤
*
* 说明:属性的显式赋值与代码块中的赋值的先后顺序完全取决于声明的先后顺序。
*
*/
public class FieldTest {
public static void main(String[] args) {
Order o1 = new Order();
System.out.println(o1.orderId);
}
}
class Order{
int orderId = 1;
{
System.out.println("代码块");
orderId = 2;
}
public Order(){
orderId = 3;
}
}
练习
//口诀:由父及子,静态先行
class Root{
static{
System.out.println("Root的静态初始化块");
}
{
System.out.println("Root的普通初始化块");
}
public Root(){
System.out.println("Root的无参数的构造器");
}
}
class Mid extends Root{
static{
System.out.println("Mid的静态初始化块");
}
{
System.out.println("Mid的普通初始化块");
}
public Mid(){
super();
System.out.println("Mid的无参数的构造器");
}
public Mid(String msg){
//通过this调用同一类中重载的构造器
this();
System.out.println("Mid的带参数构造器,其参数值:"
+ msg);
}
}
class Leaf extends Mid{
static{
System.out.println("Leaf的静态初始化块");
}
{
System.out.println("Leaf的普通初始化块");
}
public Leaf(){
//通过super调用父类中有一个字符串参数的构造器
super("尚硅谷");
System.out.println("Leaf的构造器");
}
}
public class LeafTest{
public static void main(String[] args){
new Leaf();
System.out.println();
new Leaf();
}
}
练习
class Father {
static {
System.out.println("11111111111");
}
{
System.out.println("22222222222");
}
public Father() {
System.out.println("33333333333");
}
}
public class Son extends Father {
static {
System.out.println("44444444444");
}
{
System.out.println("55555555555");
}
public Son() {
System.out.println("66666666666");
}
public static void main(String[] args) {
System.out.println("77777777777");
System.out.println("************************");
new Son();
System.out.println("************************");
new Son();
System.out.println("************************");
new Father();
}
}
public class MainTest {
public static void main(String[] args) {
Main.main(null);
}
}
class Main{
public static void main(String[] args) {
System.out.println("hello");
}
}
public class FinalTest {
public static void main(String[] args) {
FinalTest test = new FinalTest();
// test.num3 = 10;
FinalTest.method1(10);
}
final int num = 1;
final int num1;
final int num2;
// final int num3;
{
// num = 1;
num1 = 1;
}
public FinalTest(){
num2 = 2;
}
public FinalTest(String s){
this();
}
public void method(){
final int NUM = 1;
// NUM = 2;
System.out.println(NUM);
}
public static void method1(final int NUM){
// NUM = 1;
System.out.println(NUM);
}
}
//class SubString extends String{
//
//}
final class A{
}
//class B extends A{
//
//}
class C{
public final void method(){
}
}
class D extends C{
// public void method(){
//
// }
}
public class AbstractTest {
public static void main(String[] args) {
// Person p1 = new Person();
// p1.eat();
Student s1 = new Student();
s1.eat();
s1.walk();
}
}
abstract class Creature{
public abstract void breath();//呼吸
}
abstract class Person extends Creature{
String name;
int age;
public abstract void eat();
public void walk(){
System.out.println("人:走路");
}
}
class Student extends Person{
String major;
public void eat(){
System.out.println("学生:吃饭");
}
public void walk(){
System.out.println("学生:走路");
}
@Override
public void breath() {
System.out.println("学生应该呼吸新鲜空气");
}
}
abstract class Worker extends Person{
}
补充说明
不能用abstract修饰变量、代码块、构造器;
不能用abstract修饰私有方法、静态方法、final的方法、final的类。
抽象的应用:模板方法的设计模式
/**
* 抽象类的应用:模板方法的设计模式
*
*/
public class TemplateTest {
public static void main(String[] args) {
Template temp = new SubTemplate1();
temp.spendTime();
}
}
abstract class Template{
/**
* 计算某段代码执行所需要的花费的时间
*/
public void spendTime(){
long start = System.currentTimeMillis();
//执行某段代码
this.code();
long end = System.currentTimeMillis();
System.out.println("花费的时间为:" + (end - start));
}
protected abstract void code();
}
class SubTemplate1 extends Template{
@Override
protected void code() {
boolean isFlag = true;
for(int i = 2 ;i <= 10000;i++){
for(int j = 2;j <= Math.sqrt(i);j++){
if(i % j == 0){
isFlag = false;
break;
}
}
if(isFlag){
System.out.println(i);
}
isFlag = true;
}
}
}
class SubTemplate2 extends Template{
@Override
protected void code() {
}
}
练习1
/**
* 几何图形类
*
*/
public abstract class GeometricObject {
protected String color;//颜色
protected double weight;//权重
// protected GeometricObject(){}
protected GeometricObject(String color, double weight) {
this.color = color;
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public abstract double findArea();
}
public class Circle extends GeometricObject {
private double radius;
public Circle(double radius,String color,double weight){
super(color,weight);
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double findArea(){
return 3.14 * radius * radius;
}
}
public class MyRectangle extends GeometricObject {
private double width;//宽
private double height;//高
public MyRectangle(double width, double height,String color, double weight) {
super(color, weight);
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double findArea(){
return width * height;
}
}
/**
* 定义一个测试类GeometricObjectTest,
* 编写equalsArea方法测试两个对象的面积是否相等(注意方法的参数类型,利用动态绑定技术),
* 编写displayGeometricObject方法显示对象的面积(注意方法的参数类型,利用动态绑定技术)。
*/
public class GeometricObjectTest {
public static void main(String[] args) {
GeometricObjectTest test = new GeometricObjectTest();
Circle circle = new Circle(2.3, "red", 1.0);
test.displayGeometricObject(circle);
MyRectangle myRectangle = new MyRectangle(3.4, 2.3, "blue", 2.0);
test.displayGeometricObject(myRectangle);
test.equalsArea(circle,myRectangle);
}
public void equalsArea(GeometricObject o1,GeometricObject o2){
boolean isEqual = o1.findArea() == o2.findArea();
if(isEqual){
System.out.println("面积相等");
}else{
System.out.println("面积不相等");
}
}
public void displayGeometricObject(GeometricObject o ){
System.out.println("几何图形的面积为:" + o.findArea());
}
}
练习2
public abstract class Employee {
private String name;
private int id;
private double salary;//工资
public Employee(String name, int id, double salary) {
this.name = name;
this.id = id;
this.salary = salary;
}
public Employee() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public abstract void work();
}
public class Manager extends Employee {
private double bonus;//奖金
public Manager() {
}
public Manager(double bonus) {
this.bonus = bonus;
}
public Manager(String name, int id, double salary, double bonus) {
super(name, id, salary);
this.bonus = bonus;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
@Override
public void work() {
System.out.println("管理者监督员工的工作");
}
}
public class CommonEmployee extends Employee{
public CommonEmployee(String name, int id, double salary) {
super(name, id, salary);
}
public CommonEmployee() {
}
@Override
public void work() {
System.out.println("普通员工在一线工作");
}
}
package com.atguigu.exer1;
public class EmployeeTest {
public static void main(String[] args) {
Manager manager = new Manager("Tom", 1001, 3000, 2000);
manager.work();
CommonEmployee commonEmployee = new CommonEmployee();
commonEmployee.work();
}
}
练习3
/**
*
* 定义一个Employee类,该类包含:
* private成员变量name,number,birthday,其中birthday 为MyDate类的对象;
* abstract方法earnings();
* toString()方法输出对象的name,number和birthday。
*
*/
abstract class Employee {
private String name;//员工姓名
private String number;//员工工号
private MyDate birthday;//员工生日
public Employee() {
}
public Employee(String name, String number) {
this.name = name;
this.number = number;
}
public Employee(String name, String number, MyDate birthday) {
this.name = name;
this.number = number;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public MyDate getBirthday() {
return birthday;
}
/**
* 发工资方式
* @return 返回员工的工资
*/
public abstract double earnings();
@Override
public String toString() {
return "{" +
"name='" + name + '\'' +
", number='" + number + '\'' +
", birthday=" + birthday.toDateString() +
'}';
}
}
package com.atguigu.exer2;
/**
*
* MyDate类包含:
* private成员变量year,month,day ;
* toDateString()方法返回日期对应的字符串:xxxx年xx月xx日
*
*/
public class MyDate {
private int year;
private int month;
private int day;
public MyDate() {
}
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public String toDateString() {
return year + "年" + month + "月" + day + "日";
}
}
/**
* 定义SalariedEmployee类继承Employee类,实现按月计算工资的员工处理。
* 该类包括:private成员变量monthlySalary;
* 实现父类的抽象方法earnings(),该方法返回monthlySalary值;
* toString()方法输出员工类型信息及员工的name,number,birthday。
*
* 月结工资
*/
public class SalariedEmployee extends Employee {
private double monthlySalary;//月工资
public SalariedEmployee() {
}
public SalariedEmployee(String name, String number, double monthlySalary) {
super(name, number);
this.monthlySalary = monthlySalary;
}
public SalariedEmployee(String name, String number, MyDate birthday, double monthlySalary) {
super(name, number, birthday);
this.monthlySalary = monthlySalary;
}
/**
* 返回员工工资,月结
* @return
*/
@Override
public double earnings() {
return monthlySalary;
}
public String toString(){
return "SalariedEmployee" + super.toString() ;
}
}
/**
*
* 参照SalariedEmployee类定义HourlyEmployee类,实现按小时计算工资的员工处理。该类包括:
* private成员变量wage和hour;
* 实现父类的抽象方法earnings(),该方法返回wage*hour值;
* toString()方法输出员工类型信息及员工的name,number,birthday。
*
*
*/
public class HourlyEmployee extends Employee {
private double wage;//单位小时的工资
private int hour;//月工作小时数
public HourlyEmployee() {
}
public HourlyEmployee(String name, String number, MyDate birthday, double wage, int hour) {
super(name, number, birthday);
this.wage = wage;
this.hour = hour;
}
/**
* 返回员工的月工资
* @return
*/
@Override
public double earnings() {
return wage * hour;
}
public double getWage() {
return wage;
}
public void setWage(double wage) {
this.wage = wage;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public String toString(){
return "HourlyEmployee" + super.toString();
}
}
import java.util.Calendar;
import java.util.Scanner;
/**
* 定义PayrollSystem类,
* 创建Employee变量数组并初始化,该数组存放各类雇员对象的引用。
* 利用循环结构遍历数组元素,输出各个对象的类型,name,number,birthday,以及该对象生日。
* <p>
* 当键盘输入本月月份值时,如果本月是某个Employee对象的生日,还要输出增加工资信息。
*
*/
public class PayrollSystem {
Employee[] employees = new Employee[2];
public static void main(String[] args) {
PayrollSystem test = new PayrollSystem();
test.employees[0] = new SalariedEmployee("江学振", "1001", new MyDate(1995, 3, 4), 20000);
test.employees[1] = new HourlyEmployee("荣鹏", "1002", new MyDate(1996, 4, 3), 100, 176);
//方式1:
// Scanner scan = new Scanner(System.in);
// System.out.println("请输入当前的月份:");
// int month = scan.nextInt();
//方式2:
Calendar calendar = Calendar.getInstance();
int month = calendar.get(Calendar.MONTH) + 1;
// System.out.println(month);
for (int i = 0; i < test.employees.length; i++) {
System.out.println(test.employees[i]);
double bonus = 0;
if (month == test.employees[i].getBirthday().getMonth()) {
System.out.println("生日快乐!奖100块钱");
bonus = 100;
}
System.out.println("员工工资为:" + (test.employees[i].earnings() + bonus));
}
}
}