1. 属性赋值的先后顺序
/**
* 测试类中实例变量赋值的先后顺序
*
* 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 ( ) ;
System. out. println ( "Mid的带参数构造器,其参数值:"
+ msg) ;
}
}
class Leaf extends Mid {
static {
System. out. println ( "Leaf的静态初始化块" ) ;
}
{
System. out. println ( "Leaf的普通初始化块" ) ;
}
public Leaf ( ) {
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 ( ) ;
}
}
2. main()
/**
* main()的体会:
* 1. main()是程序的入口
* 2. 体会一下具体的关键字
* 3. 体会main()也可以作为普通的静态方法调用
* 4. 体会一下main()形参的使用 : 可以通过形参的方式,从控制台输入数据,与程序交互
以命令行为例: java MainDemo AA 123 hello
*
*/
public class MainTest {
public static void main ( String[ ] args) {
Main. main ( null) ;
}
}
class Main {
public static void main ( String[ ] args) {
System. out. println ( "hello" ) ;
}
}
3. final关键字
/**
* final关键字的使用
*
* 1. final:最终的
*
* 2. final可以用来修饰:类、方法、变量(成员变量、局部变量)
*
* 3. final修饰类:说明此类不能被继承! 比如:String\StringBuffer\StringBuilder
*
* 4. final修饰方法:说明此方法不能被重写!比如:Object类 getClass()
*
* 5. final修饰变量:表示此"变量"不能被修改,即为常量
* 5.1 final修饰成员变量
* 可以在哪些位置赋值:显式赋值;代码块中赋值;构造器中赋值;
* 说明:只能选择上述三个位置之一进行赋值。
* 标识符的规范中,常量的声明的格式:XXX_YYY_ZZZ
* static final的成员变量:全局常量。比如:Math的PI
*
*
* 5.2 final修饰局部变量
* 修饰方法内的局部变量:保证在使用此"变量"前,给此"变量"赋值
* 修饰方法形参:调用此方法时,给形参赋值。一旦赋值,在方法内就不能修改此"变量"的值。
*
*/
public class FinalTest {
public static void main ( String[ ] args) {
FinalTest test = new FinalTest ( ) ;
FinalTest. method1 ( 10 ) ;
}
final int num = 1 ;
final int num1;
final int num2;
{
num1 = 1 ;
}
public FinalTest ( ) {
num2 = 2 ;
}
public FinalTest ( String s) {
this ( ) ;
}
public void method ( ) {
final int NUM = 1 ;
System. out. println ( NUM) ;
}
public static void method1 ( final int NUM) {
System. out. println ( NUM) ;
}
}
final class A {
}
class C {
public final void method ( ) {
}
}
class D extends C {
}
笔试题
4. abstract关键字:抽象类与抽象方法
public class AbstractTest {
public static void main ( String[ ] args) {
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 ( ) {
}
}
public abstract class GeometricObject {
protected String color;
protected double weight;
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;
}
}
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
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;
}
public abstract double earnings ( ) ;
@Override
public String toString ( ) {
return "{" +
"name='" + name + '\'' +
", number='" + number + '\'' +
", birthday=" + birthday. toDateString ( ) +
'}' ;
}
}
package com. atguigu. exer2;
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 + "日" ;
}
}
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;
}
@Override
public double earnings ( ) {
return monthlySalary;
}
public String toString ( ) {
return "SalariedEmployee" + super . toString ( ) ;
}
}
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;
}
@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;
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 ) ;
Calendar calendar = Calendar. getInstance ( ) ;
int month = calendar. get ( Calendar. MONTH) + 1 ;
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) ) ;
}
}
}
5. interface关键字:接口
/**
* 接口的使用
*
* 1. 我们使用interface定义接口 (复习:我们使用class定义类)
* 2. 接口内部可以定义的结构:
* jdk7及之前:
* 全局常量:public static final 。 在实际声明时,此修饰可以省略
* 抽象方法:public abstract 。 在实际声明时,此修饰可以省略
* ********************************************
* jdk8新特性:
* 静态方法(有方法体)、默认方法(有方法体)
* jdk9新特性:
* 私有方法
*
* 说明:接口中不存在构造器的!
* 接口不能实例化
*
* 3. 类与接口之间是实现(implements)关系,而且类可以实现多个接口。
* 格式:class A extends B implements C
* A相较于B来讲,称为:子类
* A相较于C来讲,称为:实现类
*
* 4. 如果实现类实现了接口中的所有的抽象方法,则此实现类方可实例化
* 如果实现类没有实现接口中的所有的抽象方法,则此实现类仍是一个抽象类
*
* 5. 接口与接口之间是继承关系,而且可以多继承。
*
*/
public class InterfaceTest {
public static void main ( String[ ] args) {
System. out. println ( Flyable. MIN_SPEED) ;
System. out. println ( Flyable. MAX_SPEED) ;
}
}
interface Flyable {
public static final int MIN_SPEED = 1 ;
int MAX_SPEED = 7900 ;
void fly ( ) ;
}
interface Attackable {
void attack ( ) ;
}
class Plane implements Flyable {
@Override
public void fly ( ) {
System. out. println ( "飞机可以飞~" ) ;
}
}
abstract class Bullet implements Flyable , Attackable{
}
interface AA {
void method1 ( ) ;
}
interface BB {
void method2 ( ) ;
}
interface CC extends AA , BB{
}
class DD implements CC {
public void method1 ( ) {
}
public void method2 ( ) {
}
}
体现接口是一种规范
public class USBTest {
public static void main ( String[ ] args) {
Printer printer = new Printer ( ) ;
operate ( printer) ;
operate ( new KeyBoard ( ) ) ;
}
public static void operate ( USB usb) {
System. out. println ( "========检测外部设备=========" ) ;
usb. start ( ) ;
System. out. println ( "==========具体的数据传输过程===========" ) ;
usb. stop ( ) ;
}
}
interface USB {
void start ( ) ;
void stop ( ) ;
}
class Printer implements USB {
@Override
public void start ( ) {
System. out. println ( "打印机开始工作" ) ;
}
@Override
public void stop ( ) {
System. out. println ( "打印机结束工作" ) ;
}
}
class KeyBoard implements USB {
@Override
public void start ( ) {
System. out. println ( "键盘开始工作" ) ;
}
@Override
public void stop ( ) {
System. out. println ( "键盘结束工作" ) ;
}
}
6. 了解:深拷贝与浅拷贝
class Customer {
public Customer ( Account acct, int age) {
this . acct = acct;
this . age = age;
}
public Customer ( ) {
}
Account acct;
int age;
}
class Account {
double balance;
public Account ( double balance) {
this . balance = balance;
}
public Account ( ) {
}
}
public class CopyTest {
public static void main ( String[ ] args) {
Customer c1 = new Customer ( ) ;
Customer c2 = c1;
Account acct1 = new Account ( 2000 ) ;
Customer c3 = new Customer ( acct1, 20 ) ;
Customer c4 = new Customer ( acct1, 20 ) ;
Account acct2 = new Account ( 2000 ) ;
Customer c5 = new Customer ( acct2, 20 ) ;
}
}