- 学习内容
- finalize() 方法
- static 变量和方法
- 练习内容
- 课程1 有关对象生存期的任务
- 任务1 Cat 的 finalize 方法
- 任务2 僵尸猫和僵尸狗
- 任务3 Cat 和 Dog 对象:各有 50000 个对象
- 任务4 Cat 计数器
- 任务5 偶数和奇数
- 课程2 练习使用 static 方法
- 任务1 控制体重
- 任务2 类计数器
- 任务3 猫的 static 方法
- 任务4 两点之间的距离
- 任务5 ConsoleReader 类
- 任务6 StringHelper 类
- 任务7 计算器
- 课程2 最终任务
- 任务1 Cat 和 static
- 任务2 static 猫
- 任务3 最小数量的 static
- 任务4 新想法的记事本
- 任务5 任务:每只猫都有名字和母亲。
- 任务6 升序数字
Java 虚拟机在销毁对象之前调用 finalize() 方法。
该方法用于释放系统资源或执行其他清理任务。
Object 类包含 finalize() 方法,这意味着所有其他类都是如此(因为所有 Java 类都是从 Object 类派生的)。你可以直接在类中实现自己的 finalize() 方法。
finalize方法往往不会被调用,更像是一个备份。
- 类的每个实例只能存在 static 变量的一个副本,并且必须使用类名访问该副本
- 类的方法分为两种:
实例方法是在对象上调用的,并且可以访问该对象的数据。
static 方法没有该访问权限,因为它们根本没有对象引用。
static方法可以引用类的 static 变量和其他 static 方法。
static 方法无法处理非 static 方法或非 static 变量 - static方法的优点:
- 不必传递对象引用即可使用 static 方法和变量
- 有时需要只有一个变量副本
- 有时你需要先调用一个方法,然后才能创建对象
在 Cat 类中,创建 protected void finalize() throws Throwable 方法。
package zh.codegym.task.task06.task0601;
public class Cat {
protected void finalize() throws Throwable{
}
public static void main(String[] args) {
}
}
任务2 僵尸猫和僵尸狗
在每个类(Cat 和 Dog)中编写 finalize 方法,该方法将显示有关被销毁的特定对象的文本。
package zh.codegym.task.task06.task0602;
public class Cat {
public static void main(String[] args) {
}
protected void finalize() throws Throwable {
System.out.println("一个 Cat 被销毁");
}
}
class Dog {
protected void finalize() throws Throwable {
System.out.println("一个 Dog 被销毁");
}
}
任务3 Cat 和 Dog 对象:各有 50000 个对象
在循环中分别创建 50000 个 Cat 对象和 Dog 对象。
(Java 机器应开始销毁未使用的对象,并且 finalize 方法将至少被调用一次)。
package zh.codegym.task.task06.task0603;
public class Solution {
public static void main(String[] args) {
for (int i = 0; i < 50000; i++) {
Cat cat = new Cat();
cat = null;
Dog dog = new Dog();
dog = null;
}
// 在此编写你的代码
}
}
class Cat {
@Override
protected void finalize() throws Throwable {
super.finalize();
System.out.println("一个 Cat 被销毁");
}
}
class Dog {
@Override
protected void finalize() throws Throwable {
super.finalize();
System.out.println("一个 Dog 被销毁");
}
}
任务4 Cat 计数器
在 Cat 类构造方法 public Cat() 中,将 Cat 计数器(Cat 类的 static 变量 catCount)加 1。在 finalize 方法中将其减 1。
package zh.codegym.task.task06.task0604;
public class Cat {
public static int catCount = 0;
public Cat() {
catCount = catCount + 1;
}
protected void finalize(){
catCount--;
}
//在此编写你的代码
public static void main(String[] args) {
}
}
任务5 偶数和奇数
使用键盘输入一个正数。确定输入数字中的偶数和奇数的数量。
如果一个数字可被 2 整除而没有余数(即,余数为零),则该数为偶数。
然后,我们将偶数计数器(static 变量 even)加 1。
否则,该数为奇数,我们将增加奇数计数器(static 变量 odd)。
显示以下消息:“偶数:a 奇数:b”,其中 a 为偶数的数量,b 为奇数的数量。
例如,对于 4445:
偶数:3 奇数:1
package zh.codegym.task.task06.task0606;
import java.io.*;
public class Solution {
public static int even;
public static int odd;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sNum = reader.readLine();
int len = sNum.length();
int num = Integer.parseInt(sNum);
int temp = 0;
for (int i = len; i > 0; i--) {
temp = num;
for (int j = 1; j < i; j++) {
temp = temp / 10;
}
if (temp % 2 == 1) {
odd++;
} else {
even++;
}
}
System.out.println("偶数:" + even + " 奇数:" + odd);
}
}
课程2 练习使用 static 方法
任务1 控制体重
程序应读取用户输入的体重(公斤)和身高(米)。然后显示有关用户体重指数的消息。
在 Body 类中实现一个 static 方法。该方法应计算体重指数并显示以下消息:
“体重不足:BMI < 18.5”- 如果体重指数低于 18.5,
“体重正常:18.5 <= BMI < 25”- 如果体重指数介于 18.5 和 25(不含)之间,
“超重:25 <= BMI < 30”- 如果体重指数介于 25 和 30(不含)之间,
“肥胖:BMI >= 30”- 如果体重指数大于或等于 30。
package zh.codegym.task.task06.task0605;
import java.io.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bis = new BufferedReader(new InputStreamReader(System.in));
double weight = Double.parseDouble(bis.readLine());
double height = Double.parseDouble(bis.readLine());
Body.calculateBMI(weight, height);
}
public static class Body {
public static void calculateBMI(double weight, double height) {
double BMI = weight / (height * height);
if (BMI < 18.5) {
System.out.println("体重不足:BMI < 18.5");
} else if (18.5 <= BMI && BMI < 25) {
System.out.println("体重正常:18.5 <= BMI < 25");
}else if(BMI>=25 &&BMI<30){
System.out.println("超重:25 <= BMI < 30");
}else if (BMI>= 30){
System.out.println("肥胖:BMI >= 30");
}
// 在此编写你的代码
}
}
}
任务2 类计数器
在 Cat 类中声明 static int 变量 catCount。声明构造方法(如 public Cat()),它将此变量加 1。
package zh.codegym.task.task06.task0607;
public class Cat {
static int catCount = 0;
public Cat() {
catCount++;
}
//在此编写你的代码
public static void main(String[] args) {
}
}
任务3 猫的 static 方法
向 Cat 类中添加两个 static 方法:int getCatCount() 和 setCatCount(int),用于获取/更改猫的数量(变量 catCount)。
package zh.codegym.task.task06.task0608;
public class Cat {
private static int catCount = 0;
public Cat() {
catCount++;
}
public static int getCatCount() {
return catCount;
//在此编写你的代码
}
public static void setCatCount(int catCount) {
Cat.catCount =catCount;
//在此编写你的代码
}
public static void main(String[] args) {
}
}
任务4 两点之间的距离
实现 static double getDistance(x1, y1, x2, y2) 方法。该方法应计算两点之间的距离。
使用 double Math.sqrt(double a) 方法,该方法将计算所传递参数的平方根。
package zh.codegym.task.task06.task0609;
import static java.lang.Math.sqrt;
public class Util {
public static double getDistance(int x1, int y1, int x2, int y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
public static void main(String[] args) {
}
}
任务5 ConsoleReader 类
创建 ConsoleReader 类,其中包含 4 个 static 方法:
String readString() - 从键盘读取字符串
int readInt() - 从键盘读取数字
double readDouble() - 从键盘读取小数
boolean readBoolean() - 从键盘读取字符串“true”或“false”,并返回相应的 boolean 值(true 或 false)
请注意:在每个方法中,创建从控制台(BufferedReader 或 Scanner)读取数据的变量。
package zh.codegym.task.task06.task0610;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ConsoleReader {
public static String readString() throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sString = reader.readLine();
return sString;
//在此编写你的代码
}
public static int readInt() throws Exception {
//在此编写你的代码
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(reader.readLine());
return num;
}
public static double readDouble() throws Exception {
//在此编写你的代码
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
double dNum = Double.parseDouble(reader.readLine());
return dNum;
}
public static boolean readBoolean() throws Exception {
//在此编写你的代码
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
boolean bool = Boolean.parseBoolean(reader.readLine());
return bool;
}
public static void main(String[] args) {
}
}
任务6 StringHelper 类
创建 StringHelper 类,其中包含 2 个 static 方法:
String multiply(String s, int count) - 返回已重复 count 次的字符串。
String multiply(String s) - 返回已重复 5 次的字符串。
package zh.codegym.task.task06.task0611;
public class StringHelper {
public static String multiply(String s) {
String result = "";
for (int i = 0; i < 5; i++) {
result = result + s;
}
//在此编写你的代码
return result;
}
public static String multiply(String s, int count) {
String result = "";
for (int i = 0; i < count; i++) {
result = result + s;
}
//在此编写你的代码
return result;
}
public static void main(String[] args) {
}
}
任务7 计算器
创建 Calculator 类,其中包含 5 个 static 方法:
int plus(int a, int b) - 返回 a 和 b 的总和
int minus(int a, int b) - 返回 a 和 b 之间的差
int multiply(int a, int b) - 返回 a 和 b 的乘积
double divide(int a, int b) - 返回 a 除以 b 的结果
double percent(int a, int b) - 返回数字 a 的 b%
package zh.codegym.task.task06.task0612;
public class Calculator {
public static int plus(int a, int b) {
//在此编写你的代码
return a + b;
}
public static int minus(int a, int b) {
//在此编写你的代码
return a - b;
}
public static int multiply(int a, int b) {
//在此编写你的代码
return a * b;
}
public static double divide(int a, int b) {
//在此编写你的代码
return a / (double)b;
}
public static double percent(int a, int b) {
//在此编写你的代码
return a * b / 100.0;
}
public static void main(String[] args) {
}
}
课程2 最终任务
任务1 Cat 和 static
在 Cat 类中,创建 static public int 变量 catCount。
声明构造方法 public Cat()。每次创建一只猫(新的 Cat 对象)时,都将 static 变量 catCount 加 1。创建 10 个 Cat 对象,并在屏幕上显示变量 catCount 的值。
package zh.codegym.task.task06.task0613;
public class Solution {
public static void main(String[] args) {
Cat cat1 = new Cat();
Cat cat2 = new Cat();
Cat cat3= new Cat();
Cat cat4 = new Cat();
Cat cat5 = new Cat();
Cat cat6 = new Cat();
Cat cat7 = new Cat();
Cat cat8 = new Cat();
Cat cat9 = new Cat();
Cat cat10 = new Cat();
// 创建 10 个 Cat 对象
System.out.println(Cat.catCount);
// 显示变量 catCount 的值
}
public static class Cat {
public static int catCount = 0;
// 创建 static 变量 catCount
public Cat() {
catCount = catCount + 1;
}
// 声明构造方法
}
}
任务2 static 猫
1.在 Cat 类中,添加 public static ArrayList 变量 cats。
2.每次创建一只新猫(新的 Cat 对象)时,都将其添加到变量 cats。创建 10 个 Cat 对象。
3.printCats 方法应在屏幕上显示所有的猫。你需要使用变量 cats。
package zh.codegym.task.task06.task0614;
import java.util.ArrayList;
public class Cat {
public static ArrayList cats = new ArrayList<>();
//在此编写你的代码
public Cat() {
}
public static void main(String[] args) {
for (int i = 0; i < 10 ; i++) {
cats.add(new Cat());
}
//在此编写你的代码
printCats();
}
public static void printCats() {
for (int i = 0; i < cats.size(); i++) {
System.out.println(cats.get(i));
}
//在此编写你的代码
}
}
任务3 最小数量的 static
放入使代码开始工作和程序成功完成所需的最小数量的 static 修饰符。
package zh.codegym.task.task06.task0616;
public class Solution {
public static int step;
public static void main(String[] args) {
method1();
}
public static void method1() {
method2();
}
public static void method2() {
new Solution().method3();
}
public void method3() {
method4();
}
public void method4() {
step++;
for (StackTraceElement element : Thread.currentThread().getStackTrace()) {
System.out.println(element);
}
if (step > 1) {
return;
}
main(null);
}
}
任务4 新想法的记事本
1.在 Solution 类中,创建 public static Idea 类
2.在 Idea 类中,声明返回任意非空字符串的 public String getDescription() 方法
3.在 Solution 类中,创建将显示想法描述(getDescription 方法返回的内容)的 static public void printIdea(Idea idea) 方法
package zh.codegym.task.task06.task0617;
public class Solution {
public static void printIdea(Idea idea) {
System.out.println(idea.getDescription());
}
public static class Idea {
public String getDescription() {
String s = "this is an idea.";
return s;
}
}
public static void main(String[] args) {
printIdea(new Idea());
}
//在此编写你的代码
}
任务5 任务:每只猫都有名字和母亲。
创建描述此情况的类。
创建两个对象:猫女儿和猫母亲。
在屏幕上显示它们。
新任务:每只猫都有名字、父亲和母亲。
编辑 Cat 以使其反映这些关系。
创建 6 个对象:祖父(父亲的父亲)、外祖母(母亲的母亲)、父亲、母亲、儿子、女儿。
按以下顺序显示所有对象:祖父、外祖母、父亲、母亲、儿子、女儿。
package zh.codegym.task.task06.task0621;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Cat catGrandFather = new Cat(reader.readLine());
Cat catGrandMother = new Cat(reader.readLine());
Cat catFather = new Cat(reader.readLine(), catGrandFather, null);
Cat catMother = new Cat(reader.readLine(), null, catGrandMother);
Cat catSon = new Cat(reader.readLine(), catFather, catMother);
Cat catDaughter = new Cat(reader.readLine(), catFather, catMother);
}
public static class Cat {
private String name;
private Cat mother;
private Cat father;
Cat(String name) {
this.name = name;
System.out.println(this.toString());
}
Cat(String name, Cat father, Cat mother) {
this.name = name;
this.mother = mother;
this.father = father;
System.out.println(this.toString());
}
@Override
public String toString() {
if (mother == null && father == null) {
return "猫的名字为" + name + ",无母亲,无父亲";
} else if (mother == null) {
return "猫的名字为" + name + ",无母亲" + father.name + "是父亲";
} else if (father == null) {
return "猫的名字为" + name + "," + mother.name + "是母亲,无父亲";
} else {
return "猫的名字为" + name + "," + mother.name + "是母亲," + father.name + "是父亲";
}
}
}
}
任务6 升序数字
编写程序,该程序从键盘读取 5 个数字,并按升序显示这些数字。
package zh.codegym.task.task06.task0622;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList list = new ArrayList<>();
list.add(Integer.parseInt(reader.readLine()));
list.add(Integer.parseInt(reader.readLine()));
list.add(Integer.parseInt(reader.readLine()));
list.add(Integer.parseInt(reader.readLine()));
list.add(Integer.parseInt(reader.readLine()));
for (int i = 0; i < list.size(); i++) {
for (int j = i; j < list.size(); j++) {
if (list.get(i) > list.get(j)) {
int t = 0;
t = list.get(i);
list.set(i, list.get(j));
list.set(j, t);
}
}
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}



