创建多个对象
public class Test1{
public static void main(String[] args){
//new Car().fly(); // 匿名对象,只能做一次动作
Car c = new Car();
c.run();
c.fly();
c.pinpai = "Audi";
c.model = "Q7";
c.price = 70;
c.color = "black";
System.out.println(c.pinpai);
System.out.println(c.model);
System.out.println(c.price);
System.out.println(c.color);
Car c2 = new Car();
c2.run();
c2.fly();
c2.pinpai = "BMW";
c2.model = "x6";
c2.price = 60;
c2.color = "red";
System.out.println(c2.pinpai);
System.out.println(c2.model);
System.out.println(c2.price);
System.out.println(c2.color);
Car c3 = c2; // 将 c2 的地址赋给了 c3 ,存储了地址值
System.out.println(c3.pinpai); // BMW
System.out.println(c3.model); // x6
System.out.println(c3.price); // 60
System.out.println(c3.color); // red
}
}
class Car{
String pinpai;
String model;
double price;
String color;
public void run(){
System.out.println("在开车...");
}
public void fly(){
System.out.println("会飞...");
}
}
多个对象内存图如下图所示:
注:c3 保存的是 地址值
封装
封装是指隐藏对象的属性和实现细节,仅对外提供公共的访问方式。
private关键字
权限修饰符。用于修饰成员变量和成员方法,被 private 修饰类的 成员变量 或 成员方法,只能在 类 内访问。若想外界访问类内属性,需提供 set()、get()方法。
public class Test2 {
public static void main(String[] args) {
Student s = new Student();
s.study(); // play... study...
s.setAge(20);
s.setName("tom");
s.setObject("java");
System.out.println(s.getAge()); // 20
System.out.println(s.getName()); // tom
System.out.println(s.getObject());// java
}
}
class Student {
private String name;
private int age;
private String object;
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
public void setAge(int a) {
age = a;
}
public int getAge() {
return age;
}
public void setObject(String o) {
object = o;
}
public String getObject() {
return object;
}
private void play() {
System.out.println("play...");
}
public void study() {
play();
System.out.println("study...");
}
}
构造方法
构造方法是一种特殊的方法。
1、其方法名和 类名 相同。
2、主要完成类对象 的 创建 和 初始化。
3、构造方法可 重载
修饰符 类名([参数列表]){ 方法体; }
构造方法创建对象
注:类 中 会默认存在 无参构造 方法,前提是 没有提供 含参构造 方法。
创建对象时 会 自动调用 对应的 构造方法。
public class Test3 {
public static void main(String[] args) {
Person p = new Person(); // 无参构造方法
Person p2 = new Person("tom"); //有参构造方法tom
}
}
class Person{
public Person() {
System.out.println("无参构造方法");
}
// 重载 构造方法
public Person(String name) {
System.out.println("有参构造方法" + name);
}
}
构造方法赋值
public class Test4 {
public static void main(String[] args) {
Animal a = new Animal(); // 无参构造方法
Animal a2 = new Animal("tom"); // 有参构造tom
}
}
class Animal{
String name;
public Animal() {
System.out.println("无参构造方法");
}
public Animal(String n) {
name = n;
System.out.println("有参构造" + n);
}
}
构造代码块、局部代码块
构造代码块:存在于 类的内部、方法外。作用为 提取 构造方法 的 共性,每次调用 构造方法 前 会 先 调用 构造代码块
局部代码块:存在于 方法 里 ,作用为 控制 变量 的 作用范围 。
注 : 构造代码块 在 对象 创建时 被 执行
局部代码块 在 方法 被 调用时 执行
public class Test5 {
public static void main(String[] args) {
Teacher t = new Teacher(); // 构造代码块 无参构造方法:China
Teacher t2 = new Teacher("tom");// 构造代码块 有参构造方法:tomChina
t.run(); // 局部代码块:num=10
}
}
class Teacher{
String country;
{
country = "China"; // 构造代码块
System.out.println("构造代码块");
}
public Teacher() {
System.out.println("无参构造方法:" + country);
}
public Teacher(String name) {
System.out.println("有参构造方法:" + name + country);
}
public void run() {
{
int num = 10;
System.out.println("局部代码块:num=" + num);
}
// num = 0; // error,局部代码块外 不能使用 num 变量
}
}
this 关键字(一)
this 关键字 代表 本类 对象 的 一个 引用对象
this 用途一:当 成员变量 和 局部变量 同名时,用 this 调用 成员变量
public class Test6 {
public static void main(String[] args) {
Human h = new Human();
h.show();
Human h1 = new Human("tom");
}
}
class Human{
String name;
int num = 20;
public Human() {}
public Human(String name) {
this.name = name; // 拿着右侧 的 局部变量 给 左侧 的 成员变量赋值
}
public void show() {
int num = 10;
System.out.println("num = " + num); // 由于 就近原则,会调用离该行代码最近的 局部变量
System.out.println(this.num + ":成员变量");
}
}