chapter 4

This commit is contained in:
seahi 2024-10-23 12:51:53 +08:00
parent 8737a64402
commit 761fcb4905
10 changed files with 280 additions and 0 deletions

View 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());
}
}

View File

@ -0,0 +1,5 @@
package chapter4;
public class Example02 {
}

View 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();
}
}

View 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;
}
}

View 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();
}
}

View 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;
}
}

View File

@ -0,0 +1,15 @@
package chapter4.demo45;
/**
* 定义图形接口只拥有抽象方法
* 后续的圆形和长方形都实现该接口即圆形和长方形都需要实现 getArea getPerimeter 方法
* 通过接口可以制订规范规范
*/
public interface Shape {
// 获取面积省略 public abstract
double getArea();
// 获取周长省略 public abstract
double getPerimeter();
}

View 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("请输入序号选择功能:");
}
}

View 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();
}
}

View 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();
}
}