【2023级】智能医学工程专业-计算机程序设计-第04章-面向对象
作者: 阮晓龙 发布时间: 2024/9/9 18:07:26
基本信息:
章节名称:智能医学工程专业-计算机程序设计-第04章-面向对象
授课教师:信息技术学院互联网技术教学团队
完成时间:2024年09月
适用年级:2023级
适用专业:智能医学工程
文档内容:
讲稿课件(教学版):智能医学工程-计算机程序设计-课件-第04章-面向对象-教学版.pdf
讲稿课件(笔记版):智能医学工程-计算机程序设计-课件-第04章-面向对象-笔记版.pdf
讲稿课件(在线版):
学习资源:
代码:eg封装
public class SecretRecipe {
private String recipeName;
private String secretIngredient;
// 设置食谱名称
public void setRecipeName(String name) {
this.recipeName = name;
}
// 获取食谱名称
public String getRecipeName() {
return recipeName;
}
// 设置秘密配料
public void setSecretIngredient(String ingredient) {
this.secretIngredient = ingredient;
}
// 获取秘密配料
public String getSecretIngredient() {
return "这个秘密配料对外保密!";
}
}
public class Main {
public static void main(String[] args) {
SecretRecipe recipe = new SecretRecipe();
recipe.setRecipeName("妈妈的拿手菜");
recipe.setSecretIngredient("特殊香料");
System.out.println("食谱名称: " + recipe.getRecipeName());
System.out.println("秘密配料: " + recipe.getSecretIngredient());
}
}代码:eg继承
public class Animal {
void eat() {
System.out.println("这个动物在吃东西。");
}
void sleep() {
System.out.println("这个动物在睡觉。");
}
}
public class Cat extends Animal {
void meow() {
System.out.println("猫咪在喵喵叫。");
}
}
public class Main {
public static void main(String[] args) {
Cat cat = new Cat();
cat.eat();
cat.sleep();
cat.meow();
}
}代码:eg多态
public class Device {
void turnOn() {
System.out.println("设备正在开启...");
}
}
public class TV extends Device {
@Override
void turnOn() {
System.out.println("电视正在启动,准备好享受你的节目!");
}
}
public class AirConditioner extends Device {
@Override
void turnOn() {
System.out.println("空调正在开启,给你带来清凉的感觉!");
}
}
public class Main {
public static void main(String[] args) {
Device myTV = new TV();
Device myAC = new AirConditioner();
myTV.turnOn();
myAC.turnOn();
}
}代码:eg抽象
abstract class Vehicle {
abstract void start();
abstract void stop();
}
public class Car extends Vehicle {
@Override
void start() {
System.out.println("汽车发动机启动,准备出发!");
}
@Override
void stop() {
System.out.println("汽车停车,行程结束!");
}
}
public class Bike extends Vehicle {
@Override
void start() {
System.out.println("自行车开始骑行,迎风而行!");
}
@Override
void stop() {
System.out.println("自行车停下来,休息一下!");
}
}
public class Main {
public static void main(String[] args) {
Vehicle myCar = new Car();
Vehicle myBike = new Bike();
myCar.start();
myCar.stop();
myBike.start();
myBike.stop();
}
}代码:eg4.1.2
代码示例:
public class Car {
// 属性
String brand;
int speed;
// 方法
public void accelerate() {
speed += 10;
}
}代码:eg4.1.3
public class Book {
String title;
String author;
// 构造方法
public Book(String title, String author) {
this.title = title;
this.author = author;
}
// 方法
void read() {
System.out.println("正在阅读《" + title + "》作者是 " + author + " 的书。");
}
public static void main(String[] args) {
// 创建书对象
Book book = new Book("Java 编程思想", "Bruce Eckel");
// 使用书对象
book.read();
}
}代码:eg4.2.1
public class Person {
// 属性定义
String name;
int age;
// 构造方法
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}代码:eg局部变量
public class Test {
public void display() {
int num = 10; // 局部变量
System.out.println("局部变量 num 的值是: " + num);
}
public static void main(String[] args) {
Test test = new Test();
test.display();
}
}代码:eg实例变量
public class Person {
private String name; // 实例变量
private int age;
// 构造方法
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void display() {
System.out.println("名字: " + name + ", 年龄: " + age);
}
public static void main(String[] args) {
Person person1 = new Person("周杰伦", 42);
Person person2 = new Person("林俊杰", 40);
person1.display(); // 输出: 名字: 周杰伦, 年龄: 42
person2.display(); // 输出: 名字: 林俊杰, 年龄: 40
}
}代码:eg静态变量
public class Car {
private String model; // 实例变量
private static int totalCars = 0; // 类变量
// 构造方法
public Car(String model) {
this.model = model;
totalCars++; // 每创建一个新对象,类变量总数加 1
}
public static int getTotalCars() {
return totalCars; // 返回类变量
}
public static void main(String[] args) {
Car car1 = new Car("Tesla");
Car car2 = new Car("BMW");
System.out.println("总汽车数量: " + Car.getTotalCars()); // 输出: 总汽车数量: 2
}
}代码:eg常量
public class MathConstants {
public static final double PI = 3.14159; // 常量
public static void main(String[] args) {
System.out.println("圆周率: " + PI);
// PI = 3.14; // 这行代码会报错,因为常量不能被修改
}
}代码:eg变量命名规范
public class NamingConvention {
private int age; // 描述性命名
private String personName; // 使用驼峰命名法
public NamingConvention(int age, String name) {
this.age = age;
this.personName = name;
}
public void display() {
System.out.println("年龄: " + age + ", 名字: " + personName);
}
public static void main(String[] args) {
NamingConvention nc = new NamingConvention(25, "Jay Chou");
nc.display(); // 输出: 年龄: 25, 名字: Jay Chou
}
}代码:eg4.3.1
public class Calculator {
public int add(int a, int b) {
return a + b;
}
}
1、无参数方法定义和调用:
定义:
public static void method () {
// 方法体;
}
调用:
method();
2、带参数的方法:
定义:
public static void isEvenNumber(int number){
...
}
public static void getMax(int num1, int num2){
...
}
调用:
isEvenNumber(10);
getMax(10,20);
3、带返回值的方法:
定义:
public static boolean isEvenNumber( int number ) {
return true ;
}
public static int getMax( int a, int b ) {
return 100 ;
}
调用:
isEvenNumber ( 5 ) ;
boolean flag = isEvenNumber ( 5 );代码:eg4.3.2
public class Car {
String brand;
int speed;
// 无参的构造方法
public Car() {
this.brand = "abc";//写死
this.speed = 123;//写死
}
// 有参的构造方法
public Car(String brand, int speed) {
this.brand = brand;
this.speed = speed;
}
}代码:eg4.3.3
public class Printer {
public void print(String text) {
System.out.println(text);
}
public void print(int number) {
System.out.println(number);
}
}代码:eg4.3.4
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
int result = calc.add(5, 10);
System.out.println("Result: " + result);
}
}代码:eg4.3.5
public class Main {
public static void main(String[] args) {
int a = 5;
changeValue(a);
System.out.println("After change: " + a); // 输出仍然是5
}
public static void changeValue(int num) {
num = 10;
}
}代码:eg4.3.6
public class Main {
public static void main(String[] args) {
for (String arg : args) {
System.out.println("Arg: " + arg);
}
}
}代码:eg4.3.7
直接递归:
public static void main(String[] args) {
test();
}
// 定义一个方法
public static void test() {
// 直接递归方法内部调用自己
test();
}
间接递归:
public static void main(String[] args) {
test1();
}
public static void test1 () {
// 间接递归, 方法内部调用其他方法, 其他方法再调用此方法
test2();
}
private static void test2() {
test1();
}代码:eg递归问题答案
分析:
把一个复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解。
假如我们认为存在一个公式是 f(n) = 1234567*…(n-1)*n;
那么公式等价形式就是: f(n) = f(n-1) *n
如果求的是 1-5的阶乘 的结果,我们手工应该应该如何应用上述公式计算:
f(5) = f(4) * 5
f(4) = f(3) * 4
f(3) = f(2) * 3
f(2) = f(1) * 2
f(1) = 1
当f(1)时作为条件退出递归
代码:
public static void main(String[] args) {
System.out.println(f(5));
}
public static int f(int num) {
if (num == 1) {
return 1;
} else {
return num * f(num - 1);
}
}软件资源:

