chapter 4
This commit is contained in:
parent
8737a64402
commit
761fcb4905
44
src/chapter4/Example01.java
Normal file
44
src/chapter4/Example01.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package chapter4;
|
||||||
|
|
||||||
|
class Animal {
|
||||||
|
private String name;
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 定义 Dog 类继承 Animal 类
|
||||||
|
class Dog extends Animal {
|
||||||
|
private String color;
|
||||||
|
|
||||||
|
public String getColor() {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setColor(String color) {
|
||||||
|
this.color = color;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Example01 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Dog dog = new Dog();
|
||||||
|
dog.setName("牧羊犬");
|
||||||
|
dog.setAge(3);
|
||||||
|
System.out.println("名称:" + dog.getName() + ",年龄:" + dog.getAge());
|
||||||
|
}
|
||||||
|
}
|
5
src/chapter4/Example02.java
Normal file
5
src/chapter4/Example02.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package chapter4;
|
||||||
|
|
||||||
|
|
||||||
|
public class Example02 {
|
||||||
|
}
|
46
src/chapter4/Example11/Example11.java
Normal file
46
src/chapter4/Example11/Example11.java
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package chapter4.Example11;
|
||||||
|
|
||||||
|
interface Animal {
|
||||||
|
int ID = 1; // 接口默认为常量添加 public static final
|
||||||
|
String NAME = "牧羊犬"; // 接口默认为常量添加 public static final
|
||||||
|
|
||||||
|
// 定义抽象方法shout(),接口默认添加 public abstract
|
||||||
|
void shout();
|
||||||
|
|
||||||
|
// 静态方法,默认添加 public
|
||||||
|
static int getID() {
|
||||||
|
return Animal.ID;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 抽象方法
|
||||||
|
public void info();
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Action {
|
||||||
|
public void eat();
|
||||||
|
}
|
||||||
|
|
||||||
|
class Dog implements Animal, Action {
|
||||||
|
public void eat() {
|
||||||
|
System.out.println("喜欢吃骨头");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重写Animal接口中的抽象方法shout()
|
||||||
|
public void shout() {
|
||||||
|
System.out.println("汪汪...");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void info() {
|
||||||
|
System.out.println("名称:" + NAME);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Example11 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("编号:" + Animal.getID());
|
||||||
|
Dog dog = new Dog();
|
||||||
|
dog.info();
|
||||||
|
dog.shout();
|
||||||
|
dog.eat();
|
||||||
|
}
|
||||||
|
}
|
27
src/chapter4/demo45/Circle.java
Normal file
27
src/chapter4/demo45/Circle.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package chapter4.demo45;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 圆形类
|
||||||
|
* 必需实现 Shape 接口中的 getArea 和 getPerimeter 方法
|
||||||
|
*/
|
||||||
|
public class Circle implements Shape {
|
||||||
|
// 半径
|
||||||
|
private double radius = 0;
|
||||||
|
private final static double PI = 3.14;
|
||||||
|
|
||||||
|
// 有参构造
|
||||||
|
public Circle(double radius) {
|
||||||
|
this.radius = radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 返回面积
|
||||||
|
public double getArea() {
|
||||||
|
return PI * radius * radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 返回周长
|
||||||
|
public double getPerimeter() {
|
||||||
|
return 2 * PI * radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
49
src/chapter4/demo45/Main.java
Normal file
49
src/chapter4/demo45/Main.java
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package chapter4.demo45;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// 处理用户输入
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
while(true) {
|
||||||
|
// 打印菜单
|
||||||
|
ShapeCalculate.printMenu();
|
||||||
|
int select = sc.nextInt();
|
||||||
|
// 判断用户选项
|
||||||
|
switch (select) {
|
||||||
|
case 1: // 圆形
|
||||||
|
System.out.print("请输入圆形的半径:");
|
||||||
|
double radius = sc.nextDouble();
|
||||||
|
Shape circle = new Circle(radius);
|
||||||
|
ShapeCalculate.printArea(circle);
|
||||||
|
ShapeCalculate.printPerimeter(circle);
|
||||||
|
break;
|
||||||
|
case 2: // 长方形
|
||||||
|
System.out.print("请输入长方形的长和宽:");
|
||||||
|
double length = sc.nextDouble();
|
||||||
|
double width = sc.nextDouble();
|
||||||
|
Shape rectangle = new Rectangle(length, width);
|
||||||
|
ShapeCalculate.printArea(rectangle);
|
||||||
|
ShapeCalculate.printPerimeter(rectangle);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println("输入错误,请重新选择");
|
||||||
|
ShapeCalculate.printMenu();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// 询问用户是否继续
|
||||||
|
System.out.println("继续计算?(Y / N)");
|
||||||
|
String answer = sc.next();
|
||||||
|
if ("N".equalsIgnoreCase(answer)) { // 无论大小写,都识别
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("感谢您的使用,再见!");
|
||||||
|
sc.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
29
src/chapter4/demo45/Rectangle.java
Normal file
29
src/chapter4/demo45/Rectangle.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package chapter4.demo45;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 长方形类
|
||||||
|
* 必需实现 Shape 接口中的 getArea 和 getPerimeter 方法
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Rectangle implements Shape {
|
||||||
|
// 长度
|
||||||
|
private double length = 0;
|
||||||
|
// 宽度
|
||||||
|
private double width = 0;
|
||||||
|
|
||||||
|
// 有参构造
|
||||||
|
public Rectangle(double length, double width) {
|
||||||
|
this.length = length;
|
||||||
|
this.width = width;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取面积
|
||||||
|
public double getArea() {
|
||||||
|
return length * width;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取周长
|
||||||
|
public double getPerimeter() {
|
||||||
|
return 2 * length + 2 * width;
|
||||||
|
}
|
||||||
|
}
|
15
src/chapter4/demo45/Shape.java
Normal file
15
src/chapter4/demo45/Shape.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package chapter4.demo45;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 定义“图形”接口,只拥有抽象方法
|
||||||
|
* 后续的圆形和长方形都实现该接口,即圆形和长方形都需要实现 getArea 和 getPerimeter 方法
|
||||||
|
* 通过接口,可以制订“规范、规范”
|
||||||
|
*/
|
||||||
|
public interface Shape {
|
||||||
|
// 获取面积,省略 public abstract
|
||||||
|
double getArea();
|
||||||
|
// 获取周长,省略 public abstract
|
||||||
|
double getPerimeter();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
31
src/chapter4/demo45/ShapeCalculate.java
Normal file
31
src/chapter4/demo45/ShapeCalculate.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package chapter4.demo45;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图形计算器类
|
||||||
|
* 1. final 类:防止被继承
|
||||||
|
* 2. 私有构造函数(不可被实例化)
|
||||||
|
* 只有静态方法,不需要实例化,所有方法都可以通过类名调用
|
||||||
|
*/
|
||||||
|
public final class ShapeCalculate {
|
||||||
|
private ShapeCalculate() {
|
||||||
|
throw new AssertionError("不应该被实例化");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印图形面积
|
||||||
|
public static void printArea(Shape shape) {
|
||||||
|
System.out.println("面积:"+shape.getArea());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印图形周长
|
||||||
|
public static void printPerimeter(Shape shape) {
|
||||||
|
System.out.println("周长:"+shape.getPerimeter());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印计算器菜单
|
||||||
|
public static void printMenu() {
|
||||||
|
System.out.println("==========图形计算器==========");
|
||||||
|
System.out.println("1.圆形");
|
||||||
|
System.out.println("2.长方形");
|
||||||
|
System.out.print("请输入序号选择功能:");
|
||||||
|
}
|
||||||
|
}
|
15
src/chapter4/example07/Example07.java
Normal file
15
src/chapter4/example07/Example07.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package chapter4.example07;
|
||||||
|
|
||||||
|
final class Animal {
|
||||||
|
// 空
|
||||||
|
}
|
||||||
|
|
||||||
|
//class Dog extends Animal {
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
|
||||||
|
public class Example07 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// Dog dog = new Dog();
|
||||||
|
}
|
||||||
|
}
|
19
src/chapter4/example10/Example10.java
Normal file
19
src/chapter4/example10/Example10.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package chapter4.example10;
|
||||||
|
|
||||||
|
abstract class Animal {
|
||||||
|
abstract void shout();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class Dog extends Animal {
|
||||||
|
void shout() {
|
||||||
|
System.out.println("汪汪...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Example10 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Animal dog = new Dog();
|
||||||
|
dog.shout();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user