// Java编译器将尽力保证:所有变量在使用前都要进行恰当的初始化 // Java的变量有两种:引用变量和基本类型变量,后者又分为下面两种情况 // 构造器初始化: 先自动初始化,再指定初始化,最后调用构造器进行初始化
文件结构:
项目成员的初始化:
/ Java编译器将尽力保证:所有变量在使用前都要进行恰当的初始化 // Java的变量有两种:引用变量和基本类型变量,后者又分为下面两种情况 // 构造器初始化: 先自动初始化,再指定初始化,最后调用构造器进行初始化
import javax.swing.*;
class Window {
Window(int marker) {
System.out.println("Window(" + marker + ")");
}
}
class House { //执行顺序是:先自动初始化,再指定 初始化,最后调用构造器进行初始化
Window w1 = new Window(1);
House() {
System.out.println("House( )");
w3 = new Window(33);
}
Window w2 = new Window(2);
void f() {
System.out.println("f( )");
}
Window w3 = new Window(3);
}
class OrderOfInitialization {
public static void main(String[] args) {
House h = new House();
h.f();
}
}
class Bowl {
Bowl(int marker) {
System.out.println("Bowl(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}
class Cupboard {
Bowl b3 = new Bowl(3);
static Bowl b4 = new Bowl(4);
Cupboard() {
System.out.println("Cupboard()");
b4.f(2);
}
void f3(int marker) {
System.out.println("f3(" + marker + ")");
}
static Bowl b5 = new Bowl(5);
}
class StaticInitialization {
public static void main(String[] args) {
System.out.println("Creating new Cupboard");
new Cupboard();
t3.f3(1);
}
static Cupboard t3 = new Cupboard();
}
public class 成员的初始化 {
/* void f() {
int i;// (定义时未必错)
i++; // Error,i not initialized 强制程序员提供一个初始值
}*/
//数据成员为基本类型
//Java将为类的每个基本数据成员提供默认初始化
public static void main(String[] args) {
//数据成员初始化顺序
System.out.println("数据成员初始化顺序");
OrderOfInitialization value = new OrderOfInitialization();
value.main(null);
//静态数据的初始化
//静态数据只占一份存储区,static 关键字不能应 用于局部变量,只能作用于域\方法
// 对象的数据成员将会先后进行自动初始化和指定初始化, 静态数据成员也要经过这两步初始化过程。
System.out.println("静态数据的初始化");
StaticInitialization fun = new StaticInitialization();
fun.main(null);
}
}
This用法:
class Leaf {
int i = 0;
int j = 0;
Leaf Increment() {
i++;
j++;
return this; // ⑴ 返回当前对象的引用
}
void print() {
System.out.println("i=" + i);
System.out.println("j=" + j);
}
void main(String[] args) {
Leaf x = new Leaf();
x.Increment().print(); //1
}
}
// 作为调用函数的实参 静态成员函数没有this引用,不能访直接问对象的成员
class Peeler //削皮机
{
static Apple peel(Apple apple)//削皮函数
{
//apple = " ";
return apple;
}
}
class Apple {
Apple geetPeeled() {
return Peeler.peel(this); // 作为调用函数的实参
}
public static void main(String[] args) {
Apple a = new Apple().geetPeeled();
}
}
// 在构造器中调用另外一个构造器
class Flower {
int Count = 0;
String s = "initial value";
Flower(int c) {
Count = c;
}
Flower(String ss) {
s = ss;
}
Flower(String s, int pCount) {
this(pCount); //构造器只能在第一个语句调用
// this(s); //不能调用函数两次
this.s = s; // Another use of this
}
Flower() {
this("rose", 47);
}
}
public class This {
public static void main(String[] args) {
Leaf value = new Leaf();
value.main(null);
}
}
构造类函数:
Main.java
// 引入构造器是为了初始化工作
//构造器与类名相同, 不定义返回类型。因此构造器没有返回值。
//种类: (1) 无参的构造器(习惯称为默认构造器)
// java不支持默认参数
// (2) 带参的构造器
class Person {
private String name;
private int age;
// Default constructor
public Person() {
name = "Carmelo Anthony";
age = 0;
}
// Parameterized constructor
// this 关键字
// 概念:指向当前对象的引用
// 所有对象的成员函数都存放在一个共同的存储空间,通过this引用,成员函数知道它现在是被哪个对象调用的。
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getter and setter methods
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;
}
// toString method
@Override // @Override 注解用于标记一个方法,表示它是在父类或接口中定义的方法的重写版本。
// 在这个例子中,toString() 方法是重写了 Object 类中的 toString() 方法。
public String toString() {
return "Name: " + name + ", Age: " + age;
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
// 默认
Person person1 = new Person();
System.out.println(person1);
//
Person person2 = new Person("Alice", 25);
System.out.println(person2); // Output: Name: Alice, Age: 25
}
}
方法重载:
//1、 方法重载的概念
//★ 方法名相同,而参数类型列表不同
//
//◆ 依靠参数的个数、类型和顺序
// 的不同加以区分
// 不能以返回值的类型不同区分。
//
// 通过对构造器的重载,实现以多种方式初始化一个对象。
class Calculator {
// 两个整数相加
public int add(int a, int b) {
return a + b;
}
// 两个浮点数相加
public double add(double a, double b) {
return a + b;
}
// 三个整数相加
public int add(int a, int b, int c) {
return a + b + c;
}
// 两个字符串拼接
public String add(String a, String b) {
return a + b;
}
}
public class 方法重载 {
public static void main(String[] args){
Calculator calculator = new Calculator();
int intResult = calculator.add(2, 3); // 调用 add(int, int)
double doubleResult = calculator.add(2.5, 3.7); // 调用 add(double, double)
int threeIntResult = calculator.add(2, 3, 4); // 调用 add(int, int, int)
String stringResult = calculator.add("Carmelo ", "Anthony"); // 调用 add(String, String)
System.out.println(intResult);
System.out.println(doubleResult);
System.out.println(threeIntResult);
System.out.println(stringResult);
}
}
终结处理与垃圾回收:
//★ 问题1:是否存在不经new分配的内存?
//
//★ Java没有析构函数
//★ 垃圾回收器只知道释放那些经由new关键字分
// 配的内存
//◆ 但JAVA中确实可能存在着不经new分配的内存(利用本地方法调用非Java代码,如C的malloc函数)
//垃圾回收器显然不知道如何释放这块特殊的内存。
//◆ 那么这块特殊的内存如何释放呢?Java允许在
// 类中定义一个finalize()方法,可以通过调用
// 该方法来实现。
// protected void finalize( )
public class 终结处理和垃圾回收 {
public static void main(String[] args){
System.gc( );//运行垃圾回收器
}
}