java类和对象

1.定义方式与实例化

1.1定义

定义一个狗类、

//创建一个狗类
class Dog{
    //成员变量
    //成员变量未初始化有默认值
    public String name;
    public int  age;
    //狗的属性
    public void barks(){
        System.out.println(name+"wwwwwww");
    }
}

1.2实例化

public class Main{
public static void main(String[] args) {
PetDog dogh = new PetDog(); //通过new实例化对象
dogh.name = "阿黄";
dogh.color = "黑黄";
dogh.barks();
dogh.wag();
PetDog dogs = new PetDog();
dogs.name = "阿黄";
dogs.color = "黑黄";
dogs.barks();
dogs.wag();
}
}

2 this引用

1.this只能在成员方法中使用
2.this只能在非静态方法中使用
3.this也可以调用成员方法
4.this可以调用当前类的构造方法

1. 形参名不小心与成员变量名相同

public void setDay(int year, int month, int day){
year = year;
month = month;
day = day;
}

那函数体中到底是谁给谁赋值?成员变量给成员变量?参数给参数?参数给成员变量?成员变量参数?估计 自己都搞不清楚了。 

public class Date {
public int year;
public int month;
public int day;
public void setDay(int year, int month, int day){
this.year = year;
this.month = month;
this.day = day;
}
public void printDate(){
System.out.println(this.year + "/" + this.month + "/" + this.day);
}
}

public static void main(String[] args) {
Date d = new Date();
d.setDay(2020,9,15);
d.printDate();
}

this引用的是调用成员方法的对象

3.对象的构造即初始化

3 构造方法快捷

按住ctel进行要初始化的成员变量

结果

3.1构造方法

构造方法(也称为构造器)是一个特殊的成员方法,名字必须与类名相同,在创建对象时,由编译器自动调用,并且 在整个对象的生命周期内只调用一次。

public class Date {
public int year;
public int month;
public int day;
// 构造方法:
// 名字与类名相同,没有返回值类型,设置为void也不行
// 一般情况下使用public修饰
// 在创建对象时由编译器自动调用,并且在对象的生命周期内只调用一次
public Date(int year, int month, int day){
this.year = year;
this.month = month;
this.day = day;
System.out.println("Date(int,int,int)方法被调用了");
}
public void printDate(){
System.out.println(year + "-" + month + "-" + day);
}
public static void main(String[] args) {
// 此处创建了一个Date类型的对象,并没有显式调用构造方法
Date d = new Date(2021,6,9); // 输出Date(int,int,int)方法被调用了
d.printDate(); // 2021-6-9

注意:构造方法的作用就是对对象中的成员进行初始化,并不负责给对象开辟空间

3.2构造方法的特点

1. 名字必须与类名相同

2. 没有返回值类型,设置为void也不行

3. 创建对象时由编译器自动调用,并且在对象的生命周期内只调用一次(相当于人的出生,每个人只能出生一次)

4. 构造方法可以重载(用户根据自己的需求提供不同参数的构造方法

public class Date {
public int year;
public int month;
public int day;
// 无参构造方法
public Date(){
this.year = 1900;
this.month = 1;
this.day = 1;
}
// 带有三个参数的构造方法
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public void printDate(){
System.out.println(year + "-" + month + "-" + day);
}
public static void main(String[] args) {
Date d = new Date();
d.printDate();
}
}

上述两个构造方法:名字相同,参数列表不同,因此构成了方法重载。

5.如果用户没有显式定义,编译器会生成一份默认的构造方法,生成的默认构造方法一定是无参的

public class Date {
public int year;
public int month;
public int day;
public void printDate(){
System.out.println(year + "-" + month + "-" + day);
}
public static void main(String[] args) {
Date d = new Date();
d.printDate();
}
}

上述Date类中,没有定义任何构造方法,编译器会默认生成一个不带参数的构造方法

6.一旦用户定义,编译器则不再生成。

public class Date {
public int year;
public int month;
public int day;
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public void printDate(){
System.out.println(year + "-" + month + "-" + day);
}
public static void main(String[] args) {
// 如果编译器会生成,则生成的构造方法一定是无参的
// 则此处创建对象是可以通过编译的
// 但实际情况是:编译期报错
Date d = new Date();
d.printDate();
}
}
/*
Error:(26, 18) java: 无法将类 extend01.Date中的构造器 Date应用到给定类型;
需要: int,int,int
找到: 没有参数
原因: 实际参数列表和形式参数列表长度不同

7.构造方法中,可以通过this调用其他构造方法来简化代码

public int year;
public int month;
public int day;
// 无参构造方法--内部给各个成员赋值初始值,该部分功能与三个参数的构造方法重复
// 此处可以在无参构造方法中通过this调用带有三个参数的构造方法
// 但是this(1900,1,1);必须是构造方法中第一条语句
public Date(){
//System.out.println(year); 注释取消掉,编译会失败
this(1900, 1, 1);
//this.year = 1900;
//this.month = 1;
//this.day = 1;
}
// 带有三个参数的构造方法
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
}

this(...)必须是构造方法中第一条语句

3.3默认初始化

public class Date {
public int year;
public int month;
public int day;
public Date(int year, int month, int day) {
// 成员变量在定义时,并没有给初始值, 为什么就可以使用呢?
System.out.println(this.year);
System.out.println(this.month);
System.out.println(this.day);
}
public static void main(String[] args) {
// 此处a没有初始化,编译时报错:
// Error:(24, 28) java: 可能尚未初始化变量a
// int a;
// System.out.println(a);
Date d = new Date(2021,6,9);
}
}

初始化所分配的空间·

即:对象空间被申请好之后,对象中包含的成员已经设置好了初始值,比如:

3.4就地初始化

在声明成员变量时就给初始值

public class Date {
public int year = 1900;
public int month = 1;
public int day = 1;
public Date(){
}
public Date(int year, int month, int day) {
}
public static void main(String[] args) {
Date d1 = new Date(2021,6,9);
Date d2 = new Date();
}
}

4.访问限定符

5 static 成员

5.1static修饰的成员变量

static修饰的成员变量也叫做静态成员变量 静态成员变量是属于类的,是属于所有类的的变量

成员变量的特性、

1. 不属于某个具体的对象,是类的属性,所有对象共享的,不存储在某个对象的空间中

2. 既可以通过对象访问,也可以通过类名访问,但一般更推荐使用类名访问

3. 类变量存储在方法区当中

4. 生命周期伴随类的一生(即:随类的加载而创建,随类的卸载而销毁

public class Student{
public String name;
public String gender;
public int age;
public double score;
public static String classRoom = "Bit306";
// ...
public static void main(String[] args) {
// 静态成员变量可以直接通过类名访问
System.out.println(Student.classRoom);
Student s1 = new Student("Li leilei", "男", 18, 3.8);
Student s2 = new Student("Han MeiMei", "女", 19, 4.0);
Student s3 = new Student("Jim", "男", 18, 2.6);
// 也可以通过对象访问:但是classRoom是三个对象共享的
System.out.println(s1.classRoom);
System.out.println(s2.classRoom);
System.out.println(s3.classRoom);

5.2static修饰的成员方法

一般类中的数据成员都设置为private,而成员方法设置为public,那设置之后,Student类中classRoom属性如何 在类外访问呢?

public class Student{
private String name;
private String gender;
private int age;
private double score;
private static String classRoom = "Bit306";
// ...
}
public class TestStudent {
public static void main(String[] args) {
System.out.println(Student.classRoom);
}
}
编译失败:
Error:(10, 35) java: classRoom 在 extend01.Student 中是 private 访问控制

那static属性应该如何访问呢?

Java中,被static修饰的成员方法称为静态成员方法,是类的方法,不是某个对象所特有的。静态成员一般是通过 静态方法来访问的。

public class Student{
// ...
private static String classRoom = "Bit306";
// ...
public static String getClassRoom(){
return classRoom;
}
}
public class TestStudent {
public static void main(String[] args) {
System.out.println(Student.getClassRoom());
}
}

静态成员方法特性

1.不属于具体的对象,属于类方法

2.可以使用对象进行引用,也可以使用类进行使用(推荐使用类进行使用)

3.不能在静态方法中使用任何非静态成员变量

public static String getClassRoom(){
System.out.println(this);
return classRoom;
}
// 编译失败:Error:(35, 28) java: 无法从静态上下文中引用非静态 变量 this
public static String getClassRoom(){
age += 1;
return classRoom;
}
// 编译失败:Error:(35, 9) java: 无法从静态上下文中引用非静态 变量 age

4. 静态方法中不能调用任何非静态方法,因为非静态方法有this参数,在静态方法中调用时候无法传递this引用

public static String getClassRoom(){
doClass();
return classRoom;
}
// 编译报错:Error:(35, 9) java: 无法从静态上下文中引用非静态 方法 doClass

5.静态方法无法重写,所以无法实现多态

6.静态方法中比如main方法中要调用非静态的成员变量或者是非静态的方法只能用对象进行引用

(但要注意修饰符的权限

5.3static成员变量初始化

注意:静态成员变量一般不会放在构造方法中来初始化,构造方法中初始化的是与对象相关的实例属性

静态成员变量的初始化分为两种:就地初始化 和 静态代码块初始化。

1. 就地初始化

就地初始化指的是:在定义时直接给出初始值

public class Student{
private String name;
private String gender;
private int age;
private double score;
private static String classRoom = "Bit306"; 
// ...
}

2.静态代码块初始化

8 代码块

使用 {} 定义的一段代码称为代码块。

根据代码块定义的位置以及关键字,又可分为以下四种:

1.普通代码块

普通代码块:定义在方法中的代码块

public class Main{
public static void main(String[] args) {
{ //直接使用{}定义,普通方法块
int x = 10 ;
System.out.println("x1 = " +x);
}
int x = 100 ;
System.out.println("x2 = " +x);
}
}
// 执行结果
x1 = 10
x2 = 100

2.静态代码块

使用static定义的代码块称为静态代码块。一般用于初始化静态成员变量

package com.bit.www;

//import jdk.incubator.vector.VectorOperators;

public class Test2 {
    private String name;
    private int age;
    private String gan;
    private int add;
    private static String clas;
    //实例代码块
    {
        this.name="nfj";
        this.age=6;
        this.gan="gdfj";
        this.add=5;
        System.out.println("这个是实例化代码块");
    }
    //静态代码块
    static {
        clas="dfkj";
        System.out.println("这是静态代码块");
    }

    //构造方法
    public Test2(){
        System.out.println("这是构造方法");
    }
    public static void main(String[] args) {
        //测试同一包中的不同类的默认权限
//        TestDate tttt=new TestDate();
//        System.out.println(tttt.a);
//        -------------------------------
       // System.out.println(TestDate.count);
        Test2 a=new Test2();
        System.out.println("----------------");
        Test2 b=new Test2();


    }



}

3.构造代码块

构造块:定义在类中的代码块(不加修饰符)。也叫:实例代码块。构造代码块一般用于初始化实例成员变量

public class Student{
//实例成员变量
private String name;
private String gender;
private int age;
private double score;
public Student() {
System.out.println("I am Student init()!");
}
//实例代码块
{
this.name = "bit";
this.age = 12;
this.sex = "man";
System.out.println("I am instance init()!");
}
public void show(){
System.out.println("name: "+name+" age: "+age+" sex: "+sex);
}
}
public class Main {
public static void main(String[] args) {
Student stu = new Student();
stu.show();
}
}
// 运行结果
I am instance init()!
I am Student init()!
name: bit age: 12 sex: man

运行结果

注意事项

1.静态代码块不管生成多少个对象,其只会执行一次

2.静态成员变量是类的属性,因此是在JVM加载类时开辟空间并初始化的

3.如果一个类中包含多个静态代码块,在编译代码时,编译器会按照定义的先后次序依次执行(合并)

4.实例代码块只有在创建对象时才会执行

4,同步代码块

9 对象的打印

public class Person {
String name;
String gender;
int age;
public Person(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public static void main(String[] args) {
Person person = new Person("Jim","男", 18);
System.out.println(person);
}
}
// 打印结果:day20210829.Person@1b6d3586

如果想要默认打印对象中的属性该如何处理呢?答案:重写toString方法即可。

public class Person {
String name;
String gender;
int age;
public Person(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
@Override
public String toString() {
return "[" + name + "," + gender + "," + age + "]";
}
public static void main(String[] args) {
Person person = new Person("Jim","男", 18);
System.out.println(person);
}
}
// 输出结果:[Jim,男,18]

重写方法

10.利用类来交换数值

package dame9;
//交换两个数值
public class Test {
    public int val;
    @Override
    public String toString() {
        return "Test{" +
                "val=" + val +
                '}';
    }
    public static void exchange(Test t1,Test t2){
        int tmp=t1.val;
        t1.val=t2.val;
        t2.val=tmp;
    }
    public static void main(String[] args) {
        Test t1=new Test();
        Test t2=new Test();
        t1.val=1;
        t2.val=2;
        //交换之前
        System.out.println("交换之前"+"t1"+""+t1+" "+"t2"+" "+t2);

        //交换
        exchange(t1,t2);

        //交换之后
        System.out.println("交换之后"+"t1"+""+t1+" "+"t2"+" "+t2);

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值