(1)声明一个银行账户类Account
(2)在测试类的main中,创建账户类的两个对象,账号分别为“11111”和“22222”,余额分别为“1000和2000”元,利率初始值是0.035,调用方法测试。
public class Account {
private static double rate;
private String id;
private double balance;
public Account(String id, double balance) {
this.id = id;
this.balance = balance;
}
public static double getRate() {
return rate;
}
public static void setRate(double rate) {
Account.rate = rate;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double annualInterest(){
return balance * rate;
}
@Override
public String toString() {
return "Account{" +
"id='" + id + '\'' +
", balance=" + balance +
", 年利息 =" + annualInterest() +
'}';
}
}
public class Exercise1 {
public static void main(String[] args) {
Account a1 = new Account("11111",1000);
Account a2 = new Account("22222",2000);
Account.setRate(0.035);
System.out.println(a1);
System.out.println(a2);
}
}
(1)声明一个图形父类Shape,包含
(2)声明一个矩形Rectangle,继承Shape父类
(3)声明一个三角形Triangle,继承Shape父类
(4)声明图形工具类ShapeTools
(5)在测试类中:
public class Shape {
public double area(){
return 0.0;
}
public double perimeter(){
return 0.0;
}
public String toString(){
return "面积:" + area() +",周长:" + perimeter();
}
}
public class Rectangle extends Shape {
private double length;
private double width;
public Rectangle() {
}
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public double area() {
return length * width;
}
@Override
public double perimeter() {
return 2 * (length + width);
}
@Override
public String toString() {
return "长:" + length +
", 宽" + width +
"," + super.toString();
}
}
public class Triangle extends Shape {
private double a;
private double b;
private double c;
public Triangle() {
}
public Triangle(double a, double b, double c) {
if(a>0 && b> 0 && c>0 && a+b>c && b+c>a && a+c>b) {
this.a = a;
this.b = b;
this.c = c;
}
}
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public double getC() {
return c;
}
public void setC(double c) {
this.c = c;
}
@Override
public double area() {
double p = (a+b+c)/2;
return Math.sqrt(p * (p-a) * (p-b) *(p-c));
}
@Override
public double perimeter() {
return a+b+c;
}
@Override
public String toString() {
return "边长:" + a +
"," + b +
"," + c +
"," + super.toString();
}
}
public class ShapeTools {
public static int compare(Shape s1, Shape s2){
if(s1.area() > s2.area()){
return 1;
}else if(s1.area() < s2.area()){
return -1;
}
return 0;
}
public static boolean equals(Shape s1, Shape s2){
return s1.area() == s2.area() && s1.perimeter() == s2.perimeter();
}
public static void sort(Shape[] arr){
for (int i = 1; i < arr.length; i++) {
for (int j = 0; j < arr.length-i; j++) {
boolean flag = false;
if(arr[j].area()>arr[j+1].area()){
flag = true;
}else if(arr[j].area() == arr[j+1].area()){
if(arr[j].perimeter() > arr[j+1].perimeter()) {
flag = true;
}
}
if(flag){
Shape temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
public static void print(Shape[] arr){
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
public class Exercise2 {
public static void main(String[] args) {
Rectangle r1 = new Rectangle(2,3);
Rectangle r2 = new Rectangle(3,6);
Triangle t1 = new Triangle(3,4,5);
Triangle t2 = new Triangle(6,6,6);
int result = ShapeTools.compare(r1,t1);
if(result == 0){
System.out.println("r1和t1的面积相等");
}else if(result>0){
System.out.println("r1的面积大于t1的面积");
}else{
System.out.println("r1的面积小于t1的面积");
}
System.out.println("r2和t2的面积和周长是否都相等?" + ShapeTools.equals(r2,t2));
Shape[] arr = new Shape[4];
arr[0] = r1;
arr[1] = r2;
arr[2] = t1;
arr[3] = t2;
System.out.println("排序前:");
ShapeTools.print(arr);
ShapeTools.sort(arr);
System.out.println("排序后:");
ShapeTools.print(arr);
}
}
声明一个数组工具类ArrayTools,包含如下方法:
在测试类的main方法中
public class ArrayTools {
public static int binarySearch(int[] arr, int value){
for(int left=0,right=arr.length-1; left<=right;){
int mid = left + (right-left)/2;
if(value == arr[mid]){
return mid;
}else if(value < arr[mid]){
right = mid -1;
}else{
left = mid + 1;
}
}
return -1;
}
public static int[] copyOf(int[] arr, int newLength){
int[] newArr = new int[newLength];
for (int i = 0; i < newArr.length && i < arr.length; i++) {
newArr[i] = arr[i];
}
return newArr;
}
public static void sort(int[] arr){
for (int i = 1; i < arr.length; i++) {
for (int j = 0; j < arr.length-i; j++) {
if(arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
public static String toString(int[] arr){
String result = "[";
for (int i = 0; i < arr.length; i++) {
if(i==0){
result += arr[i];
}else{
result += "," + arr[i];
}
}
result +="]";
return result;
}
}
public class Exercise3 {
public static void main(String[] args) {
int[] arr = new int[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int)(Math.random()*100);
}
System.out.println(ArrayTools.toString(arr));
ArrayTools.sort(arr);
System.out.println(ArrayTools.toString(arr));
Scanner input = new Scanner(System.in);
System.out.print("请输入一个[0,100)范围的整数:");
int num = input.nextInt();
input.close();
int index = ArrayTools.binarySearch(arr, num);
if(index==-1){
System.out.println(num +"在arr中不存在");
}else{
System.out.println(num +"在arr的下标是:" + index);
}
int[] fiveElements = ArrayTools.copyOf(arr, 5);
System.out.println(ArrayTools.toString(fiveElements));
int[] tenElements = ArrayTools.copyOf(arr, 10);
System.out.println(ArrayTools.toString(tenElements));
int[] fifteenElements = ArrayTools.copyOf(arr, 15);
System.out.println(ArrayTools.toString(fifteenElements));
}
}
public class Exercise4 {
public static int binarySearch(int[] intsArray, int des) {
int left = 0;
int right = intsArray.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (intsArray[mid] == des) {
return mid;
} else if (des > intsArray[mid]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
}
public class Exercise5 {
public static int binarySearch(String[] intsArray, String des) {
int left = 0;
int right = intsArray.length-1;
while(left<=right) {
int mid = left + (right - left)/2;
if(intsArray[mid].equals(des)) {
return mid;
}else if(des.compareTo(intsArray[mid])>0){//字符串比较大小的方法
left = mid+1;
}else {
right=mid-1;
}
}
return -1;
}
}
public class Exercise6 {
public static void main(String[] args) throws InterruptedException {
System.out.println(Arrays.toString(suShu(10)));
}
public static int[] suShu(int n){
int[] arr = new int[n];
int index=0;
for (int i = 2; i < n; i++) {
boolean flag = true;
for (int j = 2; j < i; j++) {
if(i%j==0) {
flag=false;
break;
}
}
if(flag) {
arr[index++] = i;
}
}
return Arrays.copyOf(arr, index);
}
}
public class Exercise7 {
static int x, y, z;
static {
int x = 5;
x--;
}
static {
x--;
}
public static void main(String[] args) {
System.out.println("x=" + x);
z--;
method();
System.out.println("result:" + (z + y + ++z));
}
public static void method() {
y = z++ + ++z;
}
}
public class Exercise8 {
public static void main(String[] args) {
System.out.println(new SDText().x+","+new SDText().y+","+new SDText().x);
}
}
class SDText{
static SDText sd=new SDText();
static int x=3;
static int y;
public SDText(){
x++;
y++;
}
}
public class T {
public static int k = 0;
public static T t1 = new T("t1");
public static T t2 = new T("t2");
public static int i = print("i");
public static int n = 99;
public int j = print("j");
{
print("构造块");
}
static{
print("静态块");
}
public T(String str){
System.out.println((++k) + ":" + str + " i=" + i + " n=" + n);
++n;
++i;
}
public static int print(String str){
System.out.println((++k) + ":" + str + " i=" + i + " n=" + n);
++n;
return ++i;
}
public static void main(String[] args) {
}
}
/*
* 对于T来说,就完成类初始化
*
* 创建对象,调用类的实例初始化<init>()或<init>(String str)
*
* (1)静态变量的显式赋值
* k = 0;
t1 = new T("t1");
<init>(String str)
①j = print("j");
print方法代码如下:
public static int print(String str){
System.out.println((++k) + ":" + str + " i=" + i + " n=" + n); 1:j i=0 n=0
++n; n=1 k=1
return ++i; i=1
}
② {
print("构造块");
print方法代码如下:
public static int print(String str){
System.out.println((++k) + ":" + str + " i=" + i + " n=" + n); 2:构造块 i=1 n=1
++n; n=2 k=2
return ++i; i=2
}
}
③public T(String str){
System.out.println((++k) + ":" + str + " i=" + i + " n=" + n); 3:t1 i=2 n=2
++n; n=3 k=3
++i; i=3
}
* t2 = new T("t2");
<init>(String str)
①j = print("j");
print方法代码如下:
public static int print(String str){
System.out.println((++k) + ":" + str + " i=" + i + " n=" + n); 4:j i=3 n=3
++n; n=4 k=4
return ++i; i=4
}
② {
print("构造块");
print方法代码如下:
public static int print(String str){
System.out.println((++k) + ":" + str + " i=" + i + " n=" + n); 5:构造块 i=4 n=4
++n; n=5 k=5
return ++i; i=5
}
}
③public T(String str){
System.out.println((++k) + ":" + str + " i=" + i + " n=" + n); 6:t2 i=5 n=5
++n; n=6 k=6
++i; i=6
}
i = print("i");
print方法代码如下:
public static int print(String str){
System.out.println((++k) + ":" + str + " i=" + i + " n=" + n); 7:i i=6 n=6
++n; n=7 k=7
return ++i; i=7
}
n = 99;
* (2)静态代码块
* static{
print("静态块");
print方法代码如下:
public static int print(String str){
System.out.println((++k) + ":" + str + " i=" + i + " n=" + n); 8:静态块 i=7 n=99
++n; n=100 k=8
return ++i; i=8
}
}
*/
考核点:类初始化,局部变量与类变量,自增自减
public class Exercise10 {
static int x, y, z;
static {
int x = 5;
x--;
}
static {
x--;
}
public static void main(String[] args) {
System.out.println("x=" + x);
z--;
method();
System.out.println("result:" + (z + y + ++z));
}
public static void method() {
y = z++ + ++z;
}
}
考核点:类初始化与实例初始化
class HelloA{
public HelloA(){
System.out.println("HelloA");
}
{
System.out.println("I'm A Class");
}
static{
System.out.println("static A");
}
}
public class HelloB extends HelloA{
public HelloB(){
System.out.println("HelloB");
}
{
System.out.println("I'm B Class");
}
static{
System.out.println("static B");
}
public static void main(String[] args) {
new HelloB();
}
}
知识点:实例初始化
class HelloA{
public HelloA(){
System.out.println("HelloA");
}
{
System.out.println("I'm A Class");
}
}
class HelloB extends HelloA{
public HelloB(){
System.out.println("HelloB");
}
{
System.out.println("I'm B Class");
}
}
public class Exercise12{
public static void main(String[] args) {
new HelloB();
}
}
/*
* 创建对象是通过执行实例初始化方法来完成的。
* 如果new后面跟无参构造,就说明调用无参的实例初始化方法<init>(),
* 如果new后面跟有参构造,就说明调用有参的实例初始化方法<init>(形参列表)。
* 编译器编译后类中没有构造器,而是编译为一个个的实例初始化方法。
* 实例初始化由:
* (1)非静态成员变量的显式赋值代码
* (2)非静态代码块代码
* (3)构造器代码
* 其中(1)(2)按编写顺序,(3)在最后
* 在子类实例初始化首行会有super()或super(实参列表)表示调用父类的实例初始化方法,
* 如果没写super()或super(实参列表),那么默认就是super(),因此:
* (1)先执行父类实例初始化
* <init>(){
* System.out.println("I'm A Class");
* System.out.println("HelloA");
* }
* (2)再执行子类实例初始化
* <init>(){
* System.out.println("I'm B Class");
* System.out.println("HelloB");
* }
*/
知识点:实例初始化
public class Exercise13 {
public static void main(String[] args) {
Sub s = new Sub();
}
}
class Base{
Base(){
method(100);
}
{
System.out.println("base");
}
public void method(int i){
System.out.println("base : " + i);
}
}
class Sub extends Base{
Sub(){
super.method(70);
}
{
System.out.println("sub");
}
public void method(int j){
System.out.println("sub : " + j);
}
}
/*
* 创建对象是通过执行实例初始化方法来完成的。
* 如果new后面跟无参构造,就说明调用无参的实例初始化方法<init>(),
* 如果new后面跟有参构造,就说明调用有参的实例初始化方法<init>(形参列表)。
* 编译器编译后类中没有构造器,而是编译为一个个的实例初始化方法。
* 实例初始化由:
* (1)非静态成员变量的显式赋值代码
* (2)非静态代码块代码
* (3)构造器代码
* 其中(1)(2)按编写顺序,(3)在最后
* 在子类实例初始化首行会有super()或super(实参列表)表示调用父类的实例初始化方法,
* 如果没写super()或super(实参列表),那么默认就是super(),因此:
* 1、执行父类的实例初始化方法
* <ini>(){
* System.out.println("base");
* method(100); //因为此时在创建子类的对象过程中,所以这个method(100)方法是
* 子类对象再调用,那么又因为子类重写了method(int)方法,
* 所以执行子类的method(int)
* 即System.out.println("sub : " + j);
* }
*
* 2、执行子类的实例初始化方法
* <init>(){
* System.out.println("sub");
* super.method(70);//因为这里用super.,那么一定是调用父类的method(int)
* 即System.out.println("base : " + i);
* }
*/
public class Exercise14 {
public static void main(String[] args) {
Son son = new Son();
}
}
class Father{
static{
System.out.println("(1)父类的静态代码块");
}
{
System.out.println("(2)父类的非静态代码块");
}
Father(){
System.out.println("(3)父类的无参构造");
}
}
class Son extends Father{
static{
System.out.println("(4)子类的静态代码块");
}
{
System.out.println("(5)子类的非静态代码块");
}
Son(){
System.out.println("(6)子类的无参构造");
}
}
/*
* (1)Father类的类初始化
* ①类变量显式赋值:这里没有
* ②静态代码块
* System.out.println("(1)父类的静态代码块");
* (2)Son类的类初始化
* ①类变量显式赋值:这里没有
* ②静态代码块
* System.out.println("(4)子类的静态代码块");
*
* (3)执行Father类的是实参初始化方法<init>()
* ①非静态成员变量的显式赋值:这里没有
* ②非静态代码块:
* System.out.println("(2)父类的非静态代码块");
* ③父类的无参构造
* System.out.println("(3)父类的无参构造");
*
* (4)执行Son类的实例初始化方法<init>()
* ①非静态成员变量的显式赋值:这里没有
* ②非静态代码块:
* System.out.println("(5)子类的非静态代码块");
* ③子类的无参构造
* System.out.println("(6)子类的无参构造");
*/
public class Exercise15 {
public static void main(String[] args) {
Zi zi = new Zi();
}
}
class Fu{
private static int i = getNum("(1)i");
private int j = getNum("(2)j");
static{
print("(3)父类静态代码块");
}
{
print("(4)父类非静态代码块,又称为构造代码块");
}
Fu(){
print("(5)父类构造器");
}
public static void print(String str){
System.out.println(str + "->" + i);
}
public static int getNum(String str){
print(str);
return ++i;
}
}
class Zi extends Fu{
private static int k = getNum("(6)k");
private int h = getNum("(7)h");
static{
print("(8)子类静态代码块");
}
{
print("(9)子类非静态代码块,又称为构造代码块");
}
Zi(){
print("(10)子类构造器");
}
public static void print(String str){
System.out.println(str + "->" + k);
}
public static int getNum(String str){
print(str);
return ++k;
}
}
/*
* (1)Fu类的类初始化
* ①类变量显式赋值:
* i = getNum("(1)i");
* public static int getNum(String str){
print(str);
print方法代码如下:
public static void print(String str){
System.out.println(str + "->" + i); (1)i -> 0(默认值)
}
return ++i; i=1
}
* ②静态代码块
* static{
print("(3)父类静态代码块");
print方法代码如下:
public static void print(String str){
System.out.println(str + "->" + i); (3)父类静态代码块 -> 1
}
}
* (2)Zi类的类初始化
* ①类变量显式赋值:
* k = getNum("(6)k");
*
public static int getNum(String str){
print(str);
print方法代码如下:
public static void print(String str){
System.out.println(str + "->" + k); (6)k -> 0(默认值)
}
return ++k; k=1
}
* ②静态代码块
* static{
print("(8)子类静态代码块");
print方法代码如下:
public static void print(String str){
System.out.println(str + "->" + k); (8)子类静态代码块 -> 1
}
}
*
* (3)执行Fu类的是实参初始化方法<init>()
* ①非静态成员变量的显式赋值:
* j = getNum("(2)j");
*
public static int getNum(String str){
print(str);
print方法代码如下:
public static void print(String str){
System.out.println(str + "->" + i); (2)j -> 1
}
return ++i; i=2
}
* ②非静态代码块:
* {
print("(4)父类非静态代码块,又称为构造代码块");
print方法代码如下:
public static void print(String str){
System.out.println(str + "->" + i); (4)父类非静态代码块,又称为构造代码块 -> 2
}
}
* ③父类的无参构造
* Fu(){
print("(5)父类构造器");
print方法代码如下:
public static void print(String str){
System.out.println(str + "->" + i); (5)父类构造器 -> 2
}
}
*
* (4)执行Zi类的实例初始化方法<init>()
* ①非静态成员变量的显式赋值:
* h = getNum("(7)h");
public static int getNum(String str){
print(str);
print方法代码如下:
public static void print(String str){
System.out.println(str + "->" + k); (7)h ->1
}
return ++k; k=2
}
*
* ②非静态代码块:
* {
print("(9)子类非静态代码块,又称为构造代码块");
print方法代码如下:
public static void print(String str){
System.out.println(str + "->" + k); (9)子类非静态代码块,又称为构造代码块 ->2
}
}
* ③子类的无参构造
* Zi(){
print("(10)子类构造器");
print方法代码如下:
public static void print(String str){
System.out.println(str + "->" + k); (10)子类构造器 ->2
}
}
*/
public class Exercise16{
public static void main(String[] args){
Son s1 = new Son();//找到就是Son类的<init>()
}
}
class Father{
private int a = getNumber();
{
System.out.println("Father非静态代码块 a = " + a);
}
Father(){
System.out.println("Father()无参构造");
}
public int getNumber(){
System.out.println("Father:getNumber()");
return 1;
}
}
class Son extends Father{
{
System.out.println("Son非静态代码");
}
public Son(){
System.out.println("Son():无参构造");
}
public int getNumber(){
System.out.println("Son:getNumber()");
return 2;
}
}
/*
class Father{
private int a;
<init>(){
//super();//这里可以忽略它,因为Father类的父类是Object,它没有输出语句,不用管它
a = getNumber();
System.out.println("Father非静态代码块 a = " + a);
System.out.println("Father()无参构造");
}
public int getNumber(){
System.out.println("Father:getNumber()");
return 1;
}
}
class Son extends Father{
<init>(){
super();//它代表Father类<init>()
System.out.println("Son非静态代码");
System.out.println("Son():无参构造");
}
public int getNumber(){
System.out.println("Son:getNumber()");
return 2;
}
}
代码最终执行:
a = getNumber(); //因为我们现在在newSon类的对象,所以对象的运行时是Son类,
//而getNumber是虚方法,所以要看子类重写后的getNumber
System.out.println("Son:getNumber()");
return 2;
System.out.println("Father非静态代码块 a = " + a);
System.out.println("Father()无参构造");
System.out.println("Son非静态代码");
System.out.println("Son():无参构造");
*/
public class Exercise17 {
public static void main(String[] args) {
Sub s = new Sub();
}
}
class Base{
Base(){
this.method(100);
}
{
System.out.println("base");
}
public void method(int i){
System.out.println("base : " + i);
}
}
class Sub extends Base{
Sub(){
super.method(70);
}
{
System.out.println("sub");
}
public void method(int j){//是重写,虽然参数名不一样,但是参数的类型和个数一样,方法名也一样是重写
System.out.println("sub : " + j);
}
}
/*
先对类进行组装分析:
class Base{
<init>() {
//super(); 本题中可以忽略
System.out.println("base");
this.method(100);
}
public void method(int i){
System.out.println("base : " + i);
}
}
class Sub extends Base{
<init>(){
super();//这句话,要时刻记得它
System.out.println("sub");
super.method(70);
}
public void method(int j){
System.out.println("sub : " + j);
}
}
执行的代码是:
System.out.println("base");
this.method(100);//method是虚方法,我们现在在newSub类对象,所以执行重写的method
System.out.println("sub : " + j);
System.out.println("sub");
super.method(70); //method虽然是虚方法,但是这里有明确说执行父类被重写的method,因为前面有super.
System.out.println("base : " + i);
*/
public class Exercise18 {
public static void main(String[] args) {
Other o = new Other();
new TestOther().addOne(o);
/*
(1)new TestOther():匿名对象,作用是为了调用addOne方法。
(2)上面的实参o,把Other对象的地址值,传给了下面的addOne方法的形参o
(3)下面的addOne方法的形参o,前面有一个final修饰,
这个final在这里的意思是,形参o不能够指向新的对象,
这就意味着,形参和实参此时一定是指向同一个对象。
(4)如果形参和实参指向同一个对象,那么形参对象属性值的修改,其实就是实参对象属性值的修改
*/
System.out.println(o.i);//1
}
public void addOne(final Other o){
// o = new Other();//错误,o不能重新赋值
o.i++;
}
}
class Other{
public int i; //如果i前面有final,才是表示i的值不能被修改
}
编写代码,效果如图所示:
编写步骤:
public class Exercise19 {
public static void main(String[] args) {
C c = new C();
c.showA();
c.showB();
c.showC();
}
}
abstract class A{
protected int numa = 10;
public abstract void showA();
}
abstract class B extends A{
protected int numb = 20;
public abstract void showB();
}
class C extends B{
private int numc = 30;
@Override
public void showB() {
System.out.println("B类中numb:" + numb);
}
@Override
public void showA() {
System.out.println("A类中numa:" + numa);
}
public void showC(){
System.out.println("C类中numc:" + numc);
}
}
案例:
? 1、声明抽象父类Person,包含抽象方法public abstract void coiffure();
? 2、声明子类Woman,重写抽象方法,打印留短发
? 3、声明子类Man,重写抽象方法,打印留长发
? 4、声明测试类Exercise20,创建Person数组,存放Woman和Man对象,并遍历数组,调用coiffure()方法
public abstract class Person {
public abstract void coiffure();
}
public class Man extends Person{
@Override
public void coiffure() {
System.out.println("男人留短发");
}
}
public class Woman extends Person{
@Override
public void coiffure() {
System.out.println("女人留长发");
}
}
public class Exercise20 {
public static void main(String[] args) {
Person[] all = new Person[2];
all[0] = new Woman();
all[1] = new Man();
for (int i = 0; i < all.length; i++) {
all[i].coiffure();
}
}
}
案例:
? 1、声明抽象父类Person,包含抽象方法public abstract void eat();
? 2、声明子类中国人Chinese,重写抽象方法,打印用筷子吃饭
? 3、声明子类美国人American,重写抽象方法,打印用刀叉吃饭
? 4、声明子类印度人Indian,重写抽象方法,打印用手抓饭
? 5、声明测试类Test11,创建Person数组,存储各国人对象,并遍历数组,调用eat()方法
public class Exercise21 {
public static void main(String[] args) {
Person[] all = new Person[3];
all[0] = new Chinese();
all[1] = new American();
all[2] = new Indian();
for (int i = 0; i < all.length; i++) {
all[i].eat();
}
}
}
abstract class Person{
public abstract void eat();
}
class Chinese extends Person{
@Override
public void eat() {
System.out.println("中国人用筷子吃饭");
}
}
class American extends Person{
@Override
public void eat() {
System.out.println("美国人用刀叉吃饭");
}
}
class Indian extends Person{
@Override
public void eat() {
System.out.println("印度人用手抓饭");
}
}
(1)声明一个抽象图形父类Shape,包含
(2)声明一个矩形Rectangle,继承Shape父类
(3)声明一个三角形Triangle,继承Shape父类
(4)在测试类中:
public abstract class Shape {
public abstract double area();
public abstract double perimeter();
public String toString(){
return "面积:" + area() +",周长:" + perimeter();
}
}
public class Rectangle extends Shape {
private double length;
private double width;
public Rectangle() {
}
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public double area() {
return length * width;
}
@Override
public double perimeter() {
return 2 * (length + width);
}
@Override
public String toString() {
return "长:" + length +
", 宽" + width +
"," + super.toString();
}
}
public class Triangle extends Shape {
private double a;
private double b;
private double c;
public Triangle() {
}
public Triangle(double a, double b, double c) {
if(a>0 && b> 0 && c>0 && a+b>c && b+c>a && a+c>b) {
this.a = a;
this.b = b;
this.c = c;
}
}
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public double getC() {
return c;
}
public void setC(double c) {
this.c = c;
}
@Override
public double area() {
double p = (a+b+c)/2;
return Math.sqrt(p * (p-a) * (p-b) *(p-c));
}
@Override
public double perimeter() {
return a+b+c;
}
@Override
public String toString() {
return "边长:" + a +
"," + b +
"," + c +
"," + super.toString();
}
}
public class Exercise22 {
public static void main(String[] args) {
Shape[] arr = new Shape[4];
arr[0] = new Rectangle(2,3);
arr[1] = new Rectangle(3,6);
arr[2] = new Triangle(3,4,5);
arr[3] = new Triangle(6,6,6);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
(1)声明抽象类Vehicle交通工具
(2)声明子类Monocycle单轮车
(3)声明子类Bicycle自行车
(4)声明子类Tricycle三轮车
(5)测试类
/*
(1)声明抽象类Vehicle交通工具
- 包含int类型的wheels代表轮子的数量
- 包含有参构造Vehicle(int wheels)
- 包含抽象方法public abstract void drive()
*/
public abstract class Vehicle {
private int wheels;
public Vehicle(int wheels) {
this.wheels = wheels;
}
public int getWheels() {
return wheels;
}
public void setWheels(int wheels) {
this.wheels = wheels;
}
public abstract void drive();
@Override
public String toString() {
return "Vehicle{" +
"wheels=" + wheels +
'}';
}
}
public class Monocycle extends Vehicle {
public Monocycle(int wheels) {
super(wheels);
}
@Override
public void drive() {
System.out.println("脚踏独轮车,摇摇摆摆往前走");
}
}
public class Bicycle extends Vehicle {
public Bicycle(int wheels) {
super(wheels);
}
@Override
public void drive() {
System.out.println("脚踏双轮自行车,优哉游哉往前走");
}
}
public class Tricycle extends Vehicle {
public Tricycle(int wheels) {
super(wheels);
}
@Override
public void drive() {
System.out.println("脚踏三轮车,稳稳当当往前走");
}
}
public class Exercise23 {
public static void main(String[] args) {
Vehicle[] arr = new Vehicle[3];
arr[0] = new Monocycle(1);
arr[1] = new Bicycle(2);
arr[2] = new Tricycle(3);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
arr[i].drive();
}
}
}
interface A{
int x = 0;
}
class B{
int x = 1;
}
class C extends B implements A{
public void printX(){
System.out.println(x);
}
public static void main(String[] args) {
new C().printX();
}
}
//编译不通过,因为在C类中x有歧义。
interface A{
int x = 0;
}
class B{
int x = 1;
}
class C extends B implements A{
public void printX(){
// System.out.println(x);//有歧义,要么写super.x,要么下A.x
}
public static void main(String[] args) {
new C().printX();
}
}
interface A {
int x = 0;
}
class B {
int x = 1;
}
class C extends B implements A {
public void pX() {
System.out.println(x);
}
public static void main(String[] args) {
new C().pX();
}
}
interface Playable {
void play();
}
interface Bounceable {
void play();
}
interface Rollable extends Playable, Bounceable {
Ball ball = new Ball("PingPang");
}
class Ball implements Rollable {
private String name;
public String getName() {
return name;
}
public Ball(String name) {
this.name = name;
}
public void play() {
ball = new Ball("Football");
System.out.println(ball.getName());
}
}
public class Exercise27 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("选择:");
String role1 = input.next();
FightAble f1 = Player.select(role1);
f1.specialFight();
f1.commonFight();
System.out.println("====================");
System.out.print("选择:");
String role2 = input.next();
FightAble f2 = Player.select(role2);
f2.specialFight();
f2.commonFight();
input.close();
}
}
interface FightAble{
void specialFight();
default void commonFight(){
System.out.println("普通攻击");
}
}
class Soldier implements FightAble{
@Override
public void specialFight() {
System.out.println("武器攻击");
}
}
class Mage implements FightAble{
@Override
public void specialFight() {
System.out.println("法术攻击");
}
}
class Player{
public static FightAble select(String str){
if("法力角色".equals(str)){
return new Mage();
}else if("武力角色".equals(str)){
return new Soldier();
}
return null;
}
}
public class Exercise28 {
public static void main(String[] args) {
Worker w = new Worker();
Apple a1 = new Apple(5, "青色");
Apple a2 = new Apple(3, "红色");
w.pickApple(new CompareBig(), a1, a2);
w.pickApple(new CompareColor(), a1, a2);
}
}
class Apple{
private double size;
private String color;
public Apple(double size, String color) {
super();
this.size = size;
this.color = color;
}
public Apple() {
super();
}
public double getSize() {
return size;
}
public void setSize(double size) {
this.size = size;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return size + "-" + color;
}
}
interface CompareAble{
default void compare(Apple a1,Apple a2){
System.out.println("默认挑大的:");
if(a1.getSize() > a2.getSize()){
System.out.println(a1);
}else{
System.out.println(a2);
}
}
}
class CompareBig implements CompareAble{
}
class CompareColor implements CompareAble{
@Override
public void compare(Apple a1, Apple a2) {
System.out.println("挑红的:");
if("红色".equals(a1.getColor())){
System.out.println(a1);
}
if("红色".equals(a2.getColor())){
System.out.println(a2);
}
}
}
class Worker{
public void pickApple(CompareAble c,Apple a1,Apple a2){
c.compare(a1, a2);
}
}
class Exercise29 {
public static void main(String[] args) {
User[] all = new User[15];
for (int i = 0; i < all.length; i++) {
all[i] = new User(null,i+1);
}
V1Filter v1F = new V1Filter();
V2Filter v2F = new V2Filter();
AFilter aF = new AFilter();
Receptionist r1 = new Receptionist(v1F);
for (int i = 0; i < 5; i++) {
r1.recept(all[i]);
}
Receptionist r2 = new Receptionist(v2F);
for (int i = 5; i < 10; i++) {
r2.recept(all[i]);
}
Receptionist r3 = new Receptionist(aF);
for (int i = 10; i < 15; i++) {
r3.recept(all[i]);
}
for (int i = 0; i < all.length; i++) {
System.out.println(all[i]);
}
}
}
class User{
private String type;
private int id;
public User(String type, int id) {
super();
this.type = type;
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return id + "-" + type;
}
}
interface Filter{
void filterUser(User u);
}
class V1Filter implements Filter{
@Override
public void filterUser(User u) {
u.setType("v1");
}
}
class V2Filter implements Filter{
@Override
public void filterUser(User u) {
u.setType("v2");
}
}
class AFilter implements Filter{
@Override
public void filterUser(User u) {
u.setType("A");
}
}
class Receptionist{
private Filter filter;
public Receptionist(Filter filter) {
super();
this.filter = filter;
}
public Filter getFilter() {
return filter;
}
public void setFilter(Filter filter) {
this.filter = filter;
}
public void recept(User u){
if(u.getType() != null){
return ;
}
filter.filterUser(u);
}
}
1、声明一个LiveAble接口
2、声明动物Animal类,实现LiveAble接口。
3、声明植物Plant类,实现LiveAble接口。
4、在测试类中,分别创建两个实现类的对象,调用对应的方法。通过接口名调用静态方法
public interface LiveAble {
// 定义抽象方法
void eat();
void breathe();
//定义默认方法
default void sleep(){
System.out.println("静止不动");
}
//定义静态方法
static void drink(){
System.out.println("喝水");
}
}
public class Animal implements LiveAble {
//重写/实现接口的抽象方法
@Override
public void eat() {
System.out.println("吃东西");
}
//重写/实现接口的抽象方法
@Override
public void breathe(){
System.out.println("吸入氧气呼出二氧化碳");
}
//重写接口的默认方法
@Override
public void sleep() {
System.out.println("躺下闭上眼睛睡觉");
}
}
public class Plant implements LiveAble {
//重写/实现接口的抽象方法
@Override
public void eat() {
System.out.println("吸收营养");
}
//重写/实现接口的抽象方法
@Override
public void breathe(){
System.out.println("吸入二氧化碳呼出氧气");
}
}
public class Exercise30 {
public static void main(String[] args) {
// 创建实现类(子类)对象
Animal a = new Animal();
// 调用实现后的方法
a.eat();
a.sleep();
a.breathe();
//创建实现类(子类)对象
Plant p = new Plant();
p.eat();
p.sleep();
p.breathe();
//通过接口调用静态方法
LiveAble.drink();
}
}
(1)声明第一个接口Runner,包含抽象方法:void run()
(2)声明第二个接口Swimming,包含抽象方法:void swim()
(3)声明兔子类,实现Runner接口,
(4)声明乌龟类
实现Runner接口,重写run(),实现为“乌龟跑的快”
实现Swimming接口,重写swim()方法,实现为“乌龟游的快”
(5)测试类中创建兔子和乌龟类的对象,并调用相应方法
public interface Runner{
void run();
}
public interface Swimming {
void swim();
}
public class Rabbit implements Runner {
@Override
public void run() {
System.out.println("兔子跑得快");
}
}
public class Tortoise implements Runner,Swimming{
@Override
public void swim() {
System.out.println("乌龟游得快");
}
@Override
public void run() {
System.out.println("乌龟跑的慢");
}
}
public class Exercise31 {
public static void main(String[] args) {
Rabbit r = new Rabbit();
r.run();
Tortoise t = new Tortoise();
t.run();
t.swim();
}
}
public class Exercise32 {
public static void main(String[] args) {
B b = new B();
b.showA();
b.showB();
}
}
interface A{
void showA();
default void showB(){
System.out.println("BBB");
}
}
class B implements A{
@Override
public void showA() {
System.out.println("AAA");
}
}
定义接口Universe,提供抽象方法doAnything。
定义普通类Star,提供成员发光shine方法,打印“star:星星一闪一闪亮晶晶"
定义普通类Sun,
继承Star类,重写shine方法,打印"sun:光照八分钟,到达地球"
实现Universe接口,实现doAnything,打印"sun:太阳吸引着9大行星旋转"
测试类中,创建Star对象,调用shine方法
测试类中,多态的方式创建Sun对象,调用doAnything方法,向下转型,调用shine方法。
public class Exercise33 {
public static void main(String[] args) {
Star s = new Star();
s.shine();
System.out.println("======================");
Universe u = new Sun();
u.doAnything();
Star sun = (Star) u;
sun.shine();
}
}
interface Universe{
void doAnything();
}
class Star{
public void shine(){
System.out.println("star:星星一闪一闪亮晶晶");
}
}
class Sun extends Star implements Universe{
@Override
public void shine(){
System.out.println("sun:光照8分钟到达地球");
}
@Override
public void doAnything() {
System.out.println("sun:太阳吸引着9大行星旋转");
}
}
(1)声明Flyable接口
(2)声明Swimming接口
(3)声明类Bird
(4)声明子类Penguin企鹅
(5)声明子类Swan天鹅
(6)声明子类Chicken鸡
(7)测试类
public interface Flyable {
void fly();
}
public interface Swimming {
void swim();
}
public abstract class Bird {
public abstract void eat();
}
public class Penguin extends Bird implements Swimming {
@Override
public void eat() {
System.out.println("企鹅吃南极磷虾");
}
@Override
public void swim() {
System.out.println("企鹅下海捉虾");
}
}
public class Swan extends Bird implements Flyable,Swimming {
@Override
public void eat() {
System.out.println("天鹅吃水生植物的根茎和种子、水生昆虫、螺类和小鱼");
}
@Override
public void fly() {
System.out.println("天鹅展翅高飞,天南海北任我游");
}
@Override
public void swim() {
System.out.println("天鹅把羽毛洗的锃亮,顺便捉条鱼");
}
}
public class Chicken extends Bird implements Flyable{
@Override
public void eat() {
System.out.println("鸡吃谷子");
}
@Override
public void fly() {
System.out.println("鸡上房揭瓦,满院子乱扑腾");
}
}
public class Exercise34 {
public static void main(String[] args) {
Bird[] birds = new Bird[3];
birds[0] = new Penguin();
birds[1] = new Swan();
birds[2] = new Chicken();
for (int i = 0; i < birds.length; i++) {
birds[i].eat();
if(birds[i] instanceof Flyable){
Flyable f = (Flyable) birds[i];
f.fly();
}
if(birds[i] instanceof Swimming){
Swimming s = (Swimming)birds[i];
s.swim();
}
}
}
}
(1)已知在java.util.function包下有一个Predicate接口(这个接口不用写)
(2)声明一个数组工具类ArrayTools,包含
(3)声明一个员工类Emloyee
(4)声明一个员工管理类:EmployeeService,
包含private Employee[] arr并创建长度为5的数组
包含private int total,记录arr中员工对象个数
包含public void add(Employee emp)方法,将emp对象添加到arr数组中,
包含public Employee[] get(Predicate p)方法,这个方法的作用就是在arr数组中筛选出满足某个条件的员工对象。
(5)在测试类中,创建EmployeeService对象,
new Employee(4, "李四", 24, 24000);
new Employee(3, "张三", 23, 13000);
new Employee(5, "王五", 25, 15000);
new Employee(1, "赵六", 27, 17000);
new Employee(2, "钱七", 16, 6000);
public class ArrayTools {
public static void print(Object[] arr){
for(int i = 0;i < arr.length;i++){
System.out.println(arr[i]);
}
}
}
public class Employee {
private int id;
private String name;
private int age;
private double salary;
public Employee() {
}
public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
}
public class EmployeeService {
private Employee[] arr = new Employee[5];
private int total;
public void add(Employee emp){
if(total >= arr.length){
System.out.println("数组已满,无法添加");
return;
}
arr[total++] = emp;
}
public Employee[] get(Predicate p){
int count = 0;
for (Employee employee : arr) {
if(p.test(employee)){
count++;
}
}
Employee[] result = new Employee[count];
count = 0;
for (Employee employee : arr) {
if(p.test(employee)){
result[count++] = employee;
}
}
return result;
}
}
public class Exercise35 {
public static void main(String[] args) {
EmployeeService es = new EmployeeService();
es.add(new Employee(4, "李四", 24, 24000));
es.add(new Employee(3, "张三", 23, 13000));
es.add(new Employee(5, "王五", 25, 15000));
es.add(new Employee(1, "赵六", 27, 17000));
es.add(new Employee(2, "钱七", 16, 6000));
System.out.println("所有员工:");
Employee[] all = es.get(new Predicate() {
@Override
public boolean test(Object o) {
return true;
}
});
ArrayTools.print(all);
System.out.println("--------------------------------");
System.out.println("所有年龄超过25的员工:");
all = es.get(new Predicate() {
@Override
public boolean test(Object o) {
return ((Employee)o).getAge()>25;
}
});
ArrayTools.print(all);
System.out.println("--------------------------------");
System.out.println("所有薪资高于15000的员工:");
all = es.get(new Predicate() {
@Override
public boolean test(Object o) {
Employee e = (Employee) o;
return e.getSalary()>15000;
}
});
ArrayTools.print(all);
System.out.println("--------------------------------");
System.out.println("所有年龄超过25且薪资高于15000的员工:");
all = es.get(new Predicate() {
@Override
public boolean test(Object o) {
Employee e = (Employee) o;
return e.getAge()>25 && e.getSalary()>15000;
}
});
ArrayTools.print(all);
}
}
public class Exercise36 {
public static void main(String[] args) {
Out out = new Out();
out.Print(3);
}
}
class Out {
private int age = 12;
public void Print(final int x) {
class In {
public void inPrint() {
System.out.println(x);//3
System.out.println(age);//12
}
}
new In().inPrint();
}
}
public class Exercise37 {
public static void main(String[] args) {
Out.In in = new Out().new In();
in.print();
}
}
class Out {
private int age = 12;
class In {
private int age = 13;
public void print() {
int age = 14;
System.out.println("局部变量:" + age);//14
System.out.println("内部类变量:" + this.age);//13
System.out.println("外部类变量:" + Out.this.age);//12
}
}
}
public class Exercise38{
public Exercise38(){
Inner s1 = new Inner();
s1.a = 10;
Inner s2 = new Inner();
s2.a = 20;
Exercise38.Inner s3 = new Exercise38.Inner();
System.out.println(s3.a);
}
class Inner{
public int a = 5;
}
public static void main(String[] args) {
Exercise38 t = new Exercise38();
Inner r = t.new Inner();
System.out.println(r.a);
}
}
编写一个匿名内部类,它继承Object,并在匿名内部类中,声明一个方法public void print(),输出尚硅谷。
请编写代码调用这个方法。
public class Exercise39 {
public static void main(String[] args) {
new Object(){
public void print(){
System.out.println("尚硅谷");
}
}.print();
}
}
案例:将《05-接口第28题CompareAble接口》改用匿名内部类实现接口,来代替CompareBig和CompareColor
public class Exercise40 {
public static void main(String[] args) {
Worker w = new Worker();
Apple a1 = new Apple(5, "青色");
Apple a2 = new Apple(3, "红色");
w.pickApple(new CompareAble(){}, a1, a2);
w.pickApple(new CompareAble(){
@Override
public void compare(Apple a1, Apple a2) {
System.out.println("挑红的:");
if("红色".equals(a1.getColor())){
System.out.println(a1);
}
if("红色".equals(a2.getColor())){
System.out.println(a2);
}
}
}, a1, a2);
}
}
class Apple{
private double size;
private String color;
public Apple(double size, String color) {
super();
this.size = size;
this.color = color;
}
public Apple() {
super();
}
public double getSize() {
return size;
}
public void setSize(double size) {
this.size = size;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return size + "-" + color;
}
}
interface CompareAble{
default void compare(Apple a1,Apple a2){
System.out.println("默认挑大的:");
if(a1.getSize() > a2.getSize()){
System.out.println(a1);
}else{
System.out.println(a2);
}
}
}
class Worker{
public void pickApple(CompareAble c,Apple a1,Apple a2){
c.compare(a1, a2);
}
}
案例:将《05-接口第29题Filter接口》改用匿名内部类实现接口,来代替V1Filter、V2Filter、AFilter
public class Exercise41 {
public static void main(String[] args) {
User[] all = new User[15];
for (int i = 0; i < all.length; i++) {
all[i] = new User(null,i+1);
}
Receptionist r1 = new Receptionist(new Filter(){
@Override
public void filterUser(User u) {
u.setType("v1");
}
});
for (int i = 0; i < 5; i++) {
r1.recept(all[i]);
}
Receptionist r2 = new Receptionist(new Filter(){
@Override
public void filterUser(User u) {
u.setType("v2");
}
});
for (int i = 5; i < 10; i++) {
r2.recept(all[i]);
}
Receptionist r3 = new Receptionist(new Filter(){
@Override
public void filterUser(User u) {
u.setType("A");
}
});
for (int i = 10; i < 15; i++) {
r3.recept(all[i]);
}
for (int i = 0; i < all.length; i++) {
System.out.println(all[i]);
}
}
}
class User{
private String type;
private int id;
public User(String type, int id) {
super();
this.type = type;
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return id + "-" + type;
}
}
interface Filter{
void filterUser(User u);
}
class Receptionist{
private Filter filter;
public Receptionist(Filter filter) {
super();
this.filter = filter;
}
public Filter getFilter() {
return filter;
}
public void setFilter(Filter filter) {
this.filter = filter;
}
public void recept(User u){
if(u.getType() != null){
return ;
}
filter.filterUser(u);
}
}
(1)已知java.lang包下有一个Thread类(这个类不用写),该类有:
(2)已知java.lang包下还有一个Runnable接口(这个接口不用写),该接口有
(3)测试类
public class Exercise42 {
public static void main(String[] args) {
new Thread("康师傅"){
@Override
public void run() {
System.out.println(getName() +"爱尚硅谷");
}
}.start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("尚硅谷爱我");
}
}).start();
}
}
案例:
? 1、声明一个接口:Selector,包含抽象方法:
? (1)boolean hasNext()
? (2)Object next()
? 2、声明一个接口:Touchable,包含抽象方法:
? (1)Selector select()
? 3、声明一个MyArrayList类,当做容器类使用,模拟动态数组数据结构的容器
? (1)包含私有属性:
? ① Object[] all;用于保存对象,初始化长度为2
? ② int total;记录实际存储的对象个数
? (2)包含方法:
? ① public void add(Object element):用于添加一个元素到当前容器中,如果数组all已满,就扩容为原来的2倍
? ② public void remove(int index):如果index<0或index>=total就打印“没有这个元素”并返回,否则删除index位置的元素
? ③ public void set(int index, Object value):如果index<0或index>=total就打印“没有这个元素”并返回,否则就替换index位置的元素为value
? ④ public Object get(int index):如果index<0或index>=total就打印“没有这个元素”并返回null,否则返回index位置的元素
? ⑤ 让类MyArrayList实现Touchable接口,并重写Selector select()方法,返回内部类MySelector的对象
? ⑥ 在类MyArrayList中声明private的内部类MySelector,实现Selector接口
? A:在内部类MySelector声明一个属性:int cursor(游标)
? B:MySelector实现Selector接口,并重写两个抽象方法,其中
? > boolean hasNext()实现为:return cursor != total
? > Object next()实现为:return all[cursor++]
4、在测试类Exercise43_1中,
(1)创建MyArrayList的对象list
(2)调用list的add方法,添加3个对象
(3)调用list的remove方法,删除[1]的对象
(4)调用list的set方法,替换[1]的对象
(5)调用list的get方法,获取[1]的对象
(6)调用list的select方法,获取Selector的对象,并调用hasNext()和next()遍历容器中所有的对象
5、在测试类Exercise43_2中,
(1)声明静态的MyArrayList类型的list类变量,
(2)声明public static void init()方法,
? ①在方法中创建MyArrayList类型对象,
? ②并调用list的add()方法,添加3个对象,
? ③并在init()方法上标记JUnit4的@BeforeClass注解
(3)声明public void before()方法,
? ①打印“该测试方法开始前list中的数据如下:"
? ②调用list的select方法,获取Selector的对象,并调用hasNext()和next()遍历容器中所有的对象
? ③并在before()方法上标记JUnit4的@Before的注解
(4)声明public void after()方法,
? ①打印“该测试方法结束后list中的数据如下:"
? ②调用list的select方法,获取Selector的对象,并调用hasNext()和next()遍历容器中所有的对象
? ③并在after()方法上标记JUnit4的@After的注解
(5)声明public void testAdd()方法,
? ①在方法中,打印“现在测试的是testAdd()方法"
? ②在方法中,再次调用list的add()方法往list容器对象中添加1个对象
? ③并在testAdd()方法上标记JUnit4的@Test的注解
(6)声明public void testRemove()方法,
? ①在方法中,打印“现在测试的是testRemove()方法"
? ②调用list的remove方法,删除[1]的对象
? ③并在testRemove()方法上标记JUnit4的@Test的注解
(7)声明public void testSet()方法
? ①在方法中,打印“现在测试的是testSet()方法"
? ②调用list的set方法,替换[1]的对象
? ③并在testSet()方法上标记JUnit4的@Test的注解
(8)声明public void testGet()方法
? ①在方法中,打印“现在测试的是testGet()方法"
? ②调用list的get方法,获取[1]的对象,并打印
? ③并在testGet()方法上标记JUnit4的@Test的注解
public interface Selector {
boolean hasNext();
Object next();
}
public interface Touchable {
Selector select();
}
public class MyArrayList implements Touchable{
private Object[] all = new Object[2];
private int total;
public void add(Object element){
if(total>=all.length){
all = Arrays.copyOf(all, all.length*2);
}
all[total++] = element;
}
public void remove(int index){
if(index < 0 || index >= total){
System.out.println("没有这个元素");
return;
}
System.arraycopy(all, index+1, all, index, total-index-1);
all[--total]=null;
}
public void set(int index, Object value){
if(index < 0 || index >= total){
System.out.println("没有这个元素");
return;
}
all[index] = value;
}
public Object get(int index){
if(index < 0 || index >= total){
System.out.println("没有这个元素");
return null;
}
return all[index];
}
@Override
public Selector select() {
return new MySelector();
}
private class MySelector implements Selector{
private int cursor;
@Override
public boolean hasNext() {
return cursor != total;
}
@Override
public Object next() {
return all[cursor++];
}
}
}
public class Exercise43_1 {
public static void main(String[] args) {
MyArrayList list = new MyArrayList();
//---add()---
list.add("张三");
list.add("李四");
list.add("王五");
//---remove()---
list.remove(1);
//---set()---
list.set(1,"赵六");
//---get()---
Object obj = list.get(1);
System.out.println("[1] = " + obj);
//---select()---
Selector select = list.select();
while(select.hasNext()){
Object next = select.next();
System.out.println(next);
}
}
}
public class Exercise43_2 {
private static MyArrayList list;
@BeforeClass
public static void init(){
list = new MyArrayList();
list.add("张三");
list.add("李四");
list.add("王五");
}
@Before
public void before(){
System.out.println("该测试方法开始前list中的数据如下:");
Selector select = list.select();
while(select.hasNext()){
Object next = select.next();
System.out.println(next);
}
}
@After
public void after(){
System.out.println("该测试方法结束后list中的数据如下:");
Selector select = list.select();
while(select.hasNext()){
Object next = select.next();
System.out.println(next);
}
}
@Test
public void testAdd(){
System.out.println("现在测试的是testAdd()方法");
list.add("康师傅");
}
@Test
public void testRemove(){
System.out.println("现在测试的是testRemove()方法");
list.remove(1);
}
@Test
public void testSet(){
System.out.println("现在测试的是testSet()方法");
list.set(1,"赵六");
}
@Test
public void testGet(){
System.out.println("现在测试的是testGet()方法");
Object object = list.get(1);
System.out.println(object);
}
}
(1)已知java.lang包有一个Iterable接口(这个接口不用写),实现该接口类型的对象,就支持foreach循环遍历。Iterable接口包含:
(2)已知java.util包下有一个Iterator接口(这个接口不用写),Iterator接口包含抽象方法:
boolean hasNext()
Object next()
(3)声明一个动态数组类型MyArrayList,当做容器类使用,模拟动态数组数据结构的容器
包含私有属性:
①Object[] all;用于保存对象,初始化长度为10
? ②int total;记录实际存储的对象个数
包含方法:
①public void add(Object element):用于添加一个元素到当前容器中,如果数组all已满,不添加了
②public void remove(int index):如果index<0或index>=total就打印“没有这个元素”并返回,否则删除index位置的元素
? ③public void set(int index, Object value):如果index<0或index>=total就打印“没有这个元素”并返回,否则就替换index位置的元素为value
? ④public Object get(int index):如果index<0或index>=total就打印“没有这个元素”并返回null,否则返回index位置的元素
? ⑤让类MyArrayList实现Iterable接口,并重写Iterator iterator()方法,返回内部类Itr的对象
(4)在类MyArrayList内部中声明private的非静态内部类Itr,实现Iterator接口
(5)在测试类中,
创建MyArrayList的对象list
调用list的add方法,连续添加5个对象,分别为"atguigu",“java”,“bigdata”,“h5”,“ui”,并用foreach遍历输出
调用list的set方法,替换[1]的对象为"javaee",并用foreach遍历输出
调用list的remove方法,删除[1]的对象,并用foreach遍历输出
调用list的get方法,获取[1]的对象
调用list的iterator方法,获取Iterator接口的实现类对象,结合while循环调用hasNext()和next()遍历容器中所有的对象
public class MyArrayList implements Iterable {
private Object[] all = new Object[10];
private int total;
public void add(Object element){
if(total>=all.length){
System.out.println("数组已满,无法添加");
return;
}
all[total++] = element;
}
public void remove(int index){
if(index<0 || index>=total){
System.out.println("没有这个元素");
return;
}
for (int i = index; i < total; i++) {
all[i] = all[i+1];
}
all[--total] = null;
}
public void set(int index, Object value){
if(index<0 || index>=total){
System.out.println("没有这个元素");
return;
}
all[index] =value;
}
public Object get(int index){
if(index<0 || index>=total){
System.out.println("没有这个元素");
return null;
}
return all[index];
}
@Override
public Iterator iterator() {
return new Itr();
}
private class Itr implements Iterator{
int cursor;
@Override
public boolean hasNext() {
return cursor != total;
}
@Override
public Object next() {
return all[cursor++];
}
}
}
public class Exercise44 {
public static void main(String[] args) {
MyArrayList list = new MyArrayList();
list.add("atguigu");
list.add("java");
list.add("bigdata");
list.add("h5");
list.add("ui");
System.out.println("添加完5个元素后:");
for (Object o : list) {
System.out.println(o);
}
System.out.println("--------------------");
list.set(1,"javaee");
System.out.println("替换[1]位置的元素为javaee后");
for (Object o : list) {
System.out.println(o);
}
System.out.println("--------------------");
list.remove(1);
System.out.println("删除[1]位置的元素后");
for (Object o : list) {
System.out.println(o);
}
System.out.println("--------------------");
Object o = list.get(1);
System.out.println("[1]位置的元素现在是" + o);
System.out.println("--------------------");
Iterator iterator = list.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
(1)声明月份枚举类Month:
①声明一个属性,私有化,加final修饰
②声明一个有参构造Month(String description),创建12个常量对象,常量对象的名称如下
JANUARY,FEBRUARY,MARCH,APRIL,MAY,JUNE,JULY,AUGUST,SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER
③public static Month getByValue(int value):返回对应月份值的枚举对象,例如实参传入1,则返回JANUARY对象。
④public int length(boolean leapYear):返回这个月的总天数,如果当前对象是FEBRUARY,并且leapYear是true,则返回29,否则返回28。其他月份对象无论leapYear参数是true还是false,都正常返回月份总天数。
⑤重写toString():返回对象信息,例如:1->JANUARY->一月份。
2、在测试类中,从键盘输入年份和月份值,获取对应的月份对象,并打印月份对象,并获取月份总天数。
public enum Month {
JANUARY("一月"),
FEBRUARY("二月"),
MARCH("三月"),
APRIL("四月"),
MAY("五月"),
JUNE("六月"),
JULY("七月"),
AUGUST("八月"),
SEPTEMBER("九月"),
OCTOBER("十月"),
NOVEMBER("十一月"),
DECEMBER("十二月");
private final String description;
Month(String description) {
this.description = description;
}
public static Month getByValue(int value){
if(value>=1 && value<=12){
return Month.values()[value-1];
}
return null;
}
public int length(boolean leapYear){
switch(this){
case FEBRUARY:
return leapYear ? 29 : 28;
case APRIL:
case JUNE:
case SEPTEMBER:
case NOVEMBER:
return 30;
default:
return 31;
}
}
@Override
public String toString() {
return (ordinal()+1) +"->" + name() +"->" + description;
}
}
public class Exercise45 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入年份值:");
int year = input.nextInt();
System.out.print("请输入月份值:");
int monthValue = input.nextInt();
Month month = Month.getByValue(monthValue);
System.out.println(month);
System.out.println("月份总天数:" + month.length(year%4==0 && year%100!=0 || year%400==0));
input.close();
}
}
案例:
1、声明颜色枚举类:
2、在测试类中,使用枚举类,获取绿色对象,并打印对象。
public class Exercise46 {
public static void main(String[] args) {
Color c = Color.GREEN;
System.out.println(c);
}
}
enum Color{
RED,ORANGE,YELLOW,GREEN,CYAN,BLUE,PURPLE
}
(1)声明颜色枚举类Color:
(2)在测试类中,使用枚举类,获取绿色对象,并打印对象。
提示:
赤:(255,0,0)
橙:(255,128,0)
黄:(255,255,0)
绿:(0,255,0)
青:(0,255,255)
蓝:(0,0,255)
紫:(128,0,255)
RED, ORANGE, YELLOW, GREEN, CYAN, BLUE,PURPLE
public enum Color {
RED(255,0,0,"赤"),
ORANGE(255,128,0,"橙"),
YELLOW(255,255,0,"黄"),
GREEN(0,255,0,"绿"),
CYAN(0,255,255,"青"),
BLUE(0,0,255,"蓝"),
PURPLE(128,0,255,"紫");
private final int red;
private final int green;
private final int blue;
private final String description;
Color(int red, int green, int blue,String description) {
this.red = red;
this.green = green;
this.blue = blue;
this.description = description;
}
@Override
public String toString() {
return name()+"("+red+","+green+","+green+")->" + description;
}
}
public class Exercise47 {
public static void main(String[] args) {
Color green = Color.GREEN;
System.out.println(green);
}
}
案例:
? 1、声明月份枚举类Month:
? (1)创建:1-12月常量对象
? (2)声明两个属性:value(月份值,例如:JANUARY的value为1),
? description(描述,例如:JANUARY的description为1月份是一年的开始)。
? (3)声明一个有参构造,创建12个对象
? (4) 声明一个方法:public static Month getByValue(int value)
? (5)重写toString():返回对象信息,例如:1->JANUARY->1月份是一年的开始。
? 2、在测试类中,从键盘输入1个1-12的月份值,获取对应的月份对象,并打印对象
public class Exercise48 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入月份值(1-12):");
int m = input.nextInt();
Month month = Month.getByValue(m);
System.out.println(month);
input.close();
}
}
enum Month{
JANUARY(1,"1月份是一年的开始"),
FEBRUARY(2,"2月份是一年中最短的一个月"),
MARCH(3,"3月春暖花开"),
APRIL(4,"4月阳光明媚"),
MAY(5,"5月清凉初夏"),
JUNE(6,"6月骄阳似火"),
JULY(7,"7月下半年的第一个月"),
AUGUST(8,"8月人已晒干"),
SEPTEMBER(9,"秋风送爽"),
OCTOBER(10,"10月全国同欢"),
NOVEMBER(11,"11月寻找秋裤"),
DECMEBER(12,"12月冰天雪地");
private int value;
private String description;
private Month(int value,String description){
this.value = value;
this.description = description;
}
public static Month getByValue(int value){
return Month.values()[value-1];
}
public String toString(){
return value + "->" + name() + "->" + description;
}
}
(1)声明可支付接口Payable:
(2)声明支付枚举类Payment:
ALIPAY("支付宝"),WECHAT("微信"),CREDITCARD("信用卡"),DEPOSITCARD("储蓄卡")
? ①支付宝/微信:对接口的实现是打印“扫码支付”
? ②信用卡/储蓄卡:对接口的实现是打印“输入卡号支付”
(3)在测试类中,获取所有支付对象,打印支付对象并调用它们的pay()方法
public interface Payable {
void pay();
}
public enum Payment implements Payable{
ALIPAY("支付宝"),WECHAT("微信"),CREDITCARD("信用卡"),DEPOSITCARD("储蓄卡");
private final String description;
Payment(String description) {
this.description = description;
}
@Override
public String toString() {
return description;
}
@Override
public void pay() {
switch (this){
case ALIPAY:
case WECHAT:
System.out.println("扫码支付");
break;
case CREDITCARD:
case DEPOSITCARD:
System.out.println("输入卡号支付");
break;
}
}
}
public class Exercise49 {
public static void main(String[] args) {
Payment[] values = Payment.values();
for (int i = 0; i < values.length; i++) {
System.out.print(values[i]+":");
values[i].pay();
}
}
}
案例:
1、声明可支付接口Payable:
? 包含抽象方法:void pay();
2、声明支付枚举类Payment:
? (1)创建常量对象:支付宝(ALIPAY),微信(WECHAT),信用卡(CREDIT_CARD),储蓄卡(DEPOSIT_CARD)
? (2)枚举类Payment实现接口Payable
? ①支付宝/微信:对接口的实现是打印“扫码支付”
? ②信用卡/储蓄卡:对接口的实现是打印“输入卡号支付”
3、在测试类中,获取所有支付对象,并调用它们的pay()
public class Exercise50 {
public static void main(String[] args) {
Payment[] values = Payment.values();
for (int i = 0; i < values.length; i++) {
values[i].pay();
}
}
}
interface Payable{
void pay();
}
enum Payment implements Payable{
ALIPAY{
@Override
public void pay() {
System.out.println("扫码支付");
}
},WECHAT{
@Override
public void pay() {
System.out.println("扫码支付");
}
},CREDIT_CARD,DEPOSIT_CARD;
@Override
public void pay() {
System.out.println("输入卡号支付");
}
}
(1)声明设备状态枚举类Status
FREE("空闲"), USED("在用"), SCRAP("报废")
(2)声明设备类型Equipment
(3)现有Data.java,代码如下:
public class Data {
public static final String[][] EQUIPMENTS = {
{"1", "联想", "6000", "拯救者","0"},
{"2", "宏碁 ","5000", "AT7-N52","0"},
{"3", "小米", "2000", "5V5Pro","1"},
{"4", "戴尔", "4000", "3800-R33","1"},
{"5", "苹果", "12000", "MBP15","1"},
{"6", "华硕", "8000", "K30BD-21寸","2"},
{"7", "联想", "7000", "YOGA","0"},
{"8", "惠普", "5800", "X500","2"},
{"9", "苹果", "4500","2021Pro","0"},
{"10", "惠普", "5800", "FZ5","1"}
};
}
(4)在测试类中,创建Equipment类型的数组,并使用Data类的二维数组EQUIPMENTS的信息初始化设备对象,遍历输出
public enum Status {
FREE("空闲"), USED("在用"), SCRAP("报废");
private final int value;
private final String description;
private Status(String description) {
this.value = ordinal();
this.description = description;
}
public String getDescription() {
return description;
}
public static Status getByValue(int value){
return Status.values()[value];
}
@Override
public String toString() {
return description;
}
}
public class Equipment {
private int id;
private String factory;
private double price;
private String name;
private Status status;
public Equipment() {}
public Equipment(int id, String factory, double price, String name, Status status) {
this.id = id;
this.factory = factory;
this.price = price;
this.name = name;
this.status = status;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFactory() {
return factory;
}
public void setFactory(String factory) {
this.factory = factory;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
@Override
public String toString() {
return "Equipment{" +
"id=" + id +
", factory='" + factory + '\'' +
", name='" + name + '\'' +
", price=" + price +
", status=" + status +
'}';
}
}
public class Data {
public static final String[][] EQUIPMENTS = {
{"1", "联想", "6000", "拯救者","0"},
{"2", "宏碁 ","5000", "AT7-N52","0"},
{"3", "小米", "2000", "5V5Pro","1"},
{"4", "戴尔", "4000", "3800-R33","1"},
{"5", "苹果", "12000", "MBP15","1"},
{"6", "华硕", "8000", "K30BD-21寸","2"},
{"7", "联想", "7000", "YOGA","0"},
{"8", "惠普", "5800", "X500","2"},
{"9", "苹果", "4500","2021Pro","0"},
{"10", "惠普", "5800", "FZ5","1"}
};
}
public class Exercise51 {
public static void main(String[] args) {
Equipment[] arr = new Equipment[EQUIPMENTS.length];
for (int i = 0; i < arr.length; i++) {
int id = Integer.parseInt(EQUIPMENTS[i][0]);
String factory = EQUIPMENTS[i][1];
double price = Double.parseDouble(EQUIPMENTS[i][2]);
String name = EQUIPMENTS[i][3];
int value = Integer.parseInt(EQUIPMENTS[i][4]);
Status status = Status.getByValue(value);
arr[i] = new Equipment(id,factory,price,name,status);
System.out.println(arr[i]);
}
}
}
案例:
? 1、编写图形工具类:ShapTools
? (1)声明方法1:public static void printRectangle(),打印5行5列*组成的矩形图形
? (2)声明方法2:public static void printRectangle(int line, int column, String sign),打印line行column列由sign组成的矩形图形
? (3)给这个类加上文档注释:包含@author,@param等
? (4)给方法1标记已过时注解
? 2、编写测试类Exercise52
? 在测试类中调用上面的两个方法测试,如果有警告,就在main方法上抑制警告
public class Exercise52 {
public static void main(String[] args) {
ShapTools.printRectangle();
ShapTools.printRectangle(3, 10, "#");
}
}
class ShapTools{
@Deprecated
public static void printRectangle(){
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print("*");
}
System.out.println();
}
}
public static void printRectangle(int line, int column, String sign){
for (int i = 0; i < line; i++) {
for (int j = 0; j < column; j++) {
System.out.print(sign);
}
System.out.println();
}
}
}
public static void main(String[] args) {
Integer i1 = 128;
Integer i2 = 128;
int i3 = 128;
int i4 = 128;
System.out.println(i1 == i2);
System.out.println(i3 == i4);
System.out.println(i1 == i3);
}
package com.atguigu.test1;
public class Exercise53 {
public static void main(String[] args) {
Integer i1 = 128;
Integer i2 = 128;
int i3 = 128;
int i4 = 128;
System.out.println(i1 == i2);//false,比较地址,128超过Integer缓存对象
System.out.println(i3 == i4);//true,比较数据值
System.out.println(i1 == i3);//true,i1自动拆箱按照基本数据类型比较
//包装类对象与基本数据类型进行比较时,就会把包装类对象自动拆箱,按照基本数据类型的规则进行比较
}
}
public class Exercise54{
public static void main(String[] args) {
double a = 2.0;
double b = 2.0;
Double c = 2.0;
Double d = 2.0;
System.out.println(a == b);
System.out.println(c == d);
System.out.println(a == d);
}
}
public class Exercise54{
public static void main(String[] args) {
double a = 2.0;
double b = 2.0;
Double c = 2.0;
Double d = 2.0;
System.out.println(a == b);//true,基本数据类型比较数据值
System.out.println(c == d);//false,对象比较地址值,Double没有缓存对象
System.out.println(a == d);//true,d自动拆箱,按照基本数据类型比较
}
}
(1)现有Data.java文件
public class Data{
public static final int EMPLOYEE = 10;//表示普通员工
public static final int PROGRAMMER = 11;//表示程序员
public static final int DESIGNER = 12;//表示设计师
public static final int ARCHITECT = 13;//表示架构师
public static final String[][] EMPLOYEES = {
{"10", "1", "段誉", "22", "3000"},
{"13", "2", "令狐冲", "32", "18000", "15000", "2000"},
{"11", "3", "任我行", "23", "7000"},
{"11", "4", "张三丰", "24", "7300"},
{"12", "5", "周芷若", "28", "10000", "5000"},
{"11", "6", "赵敏", "22", "6800"},
{"12", "7", "张无忌", "29", "10800","5200"},
{"13", "8", "韦小宝", "30", "19800", "15000", "2500"},
{"12", "9", "杨过", "26", "9800", "5500"},
{"11", "10", "小龙女", "21", "6600"},
{"11", "11", "郭靖", "25", "7100"},
{"12", "12", "黄蓉", "27", "9600", "4800"}
};
}
(2)声明员工类、程序员类、设计师类、架构师类,关系如下:
(2-1)普通员工Employee类
(2-2)程序员Programmer类,继承普通员工类
(2-3)设计师Designer类,继承程序员类
(2-4)架构师Architect类,继承设计师类
(3)在测试类中创建员工数组,用Data类中的二维数组数据进行员工数组的初始化
提示:把字符串转为int和double类型的值,可以使用如下方式:
String idStr = "1";
int id = Integer.parseInt(idStr);
String salaryStr = "7300";
double salary = Double.parseDouble(salaryStr);
(4)遍历数组,使用如下格式
public class Employee {
private int id;
private String name;//姓名
private int age;//年龄
private double salary;//工资
public Employee() {
}
public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getBasicInfo(){
return id + "\t\t" + name + "\t" + age + "\t\t" + salary;
}
public String toString() {
return getBasicInfo();
}
}
public class Programmer extends Employee {
public Programmer() {
}
public Programmer(int id, String name, int age, double salary) {
super(id, name, age, salary);
}
@Override
public String toString() {
return super.getBasicInfo() + "\t\t程序员";
}
}
public class Designer extends Programmer {
private double bonus;// : 奖金
public Designer() {
}
public Designer(int id, String name, int age, double salary, double bonus) {
super(id, name, age, salary);
this.bonus = bonus;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
@Override
public String toString() {
return super.getBasicInfo() + "\t\t设计师" + "\t" + bonus;
}
}
public class Architect extends Designer {
private int stock;// : 股票
public Architect() {
}
public Architect(int id, String name, int age, double salary, double bonus, int stock) {
super(id, name, age, salary, bonus);
this.stock = stock;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
@Override
public String toString() {
return super.getBasicInfo() + "\t\t架构师" + "\t" + getBonus() + "\t" + stock;
}
}
public class Data{
public static final int EMPLOYEE = 10;//表示普通员工
public static final int PROGRAMMER = 11;//表示程序员
public static final int DESIGNER = 12;//表示设计师
public static final int ARCHITECT = 13;//表示架构师
public static final String[][] EMPLOYEES = {
{"10", "1", "段誉", "22", "3000"},
{"13", "2", "令狐冲", "32", "18000", "15000", "2000"},
{"11", "3", "任我行", "23", "7000"},
{"11", "4", "张三丰", "24", "7300"},
{"12", "5", "周芷若", "28", "10000", "5000"},
{"11", "6", "赵敏", "22", "6800"},
{"12", "7", "张无忌", "29", "10800","5200"},
{"13", "8", "韦小宝", "30", "19800", "15000", "2500"},
{"12", "9", "杨过", "26", "9800", "5500"},
{"11", "10", "小龙女", "21", "6600"},
{"11", "11", "郭靖", "25", "7100"},
{"12", "12", "黄蓉", "27", "9600", "4800"}
};
}
import static com.atguigu.wrapper.Data.*;
public class Exercise55 {
public static void main(String[] args) {
Employee[] arr = new Employee[EMPLOYEES.length];
for (int i = 0; i < arr.length; i++) {
int type = Integer.parseInt(EMPLOYEES[i][0]);
int id = Integer.parseInt(EMPLOYEES[i][1]);
String name = EMPLOYEES[i][2];
int age = Integer.parseInt(EMPLOYEES[i][3]);
double salary = Double.parseDouble(EMPLOYEES[i][4]);
switch (type){
case EMPLOYEE:
arr[i] = new Employee(id,name,age,salary);break;
case PROGRAMMER:
arr[i] = new Programmer(id,name,age,salary);break;
case DESIGNER:
double bonus = Double.parseDouble(EMPLOYEES[i][5]);
arr[i] = new Designer(id,name,age,salary,bonus);break;
case ARCHITECT:
bonus = Double.parseDouble(EMPLOYEES[i][5]);
int stock = Integer.parseInt(EMPLOYEES[i][6]);
arr[i] = new Architect(id,name,age,salary,bonus,stock);break;
}
System.out.println(arr[i]);
}
}
}