面向对象

面向对象

1.三大特性:封装、继承、多态

2.值传递、引用传递

引用传递:其实也是值传递,只不过该值为对象的地址值

package com.oop;

public class DemoTwo {
    public static void main(String[] args) {
        //引用传递:对象,本质还是值传递,但是该值为对象的地址值
        Student student = new Student();
        System.out.println(student.name);//null
        DemoTwo.change(student);
        System.out.println(student.name);//zhangsan
    }
    public static void change(Student student){
        student.name = "zhangsan";
    }
}
//定义一个Student类
class Student{
    String name;
}

3.this指针

package com.oop.DemoTwo;

public class Student {
    //属性:字段
    String name;
    int age;
    //方法
    public void study(){
        System.out.println(this.name+"在学习");
    }
}
package com.oop.DemoTwo;
//一个项目应该只存在一个main方法
public class Application {
    public static void main(String[] args) {
        //类:抽象的
        // 类实例化后会返回一个自己的对象
        Student student = new Student();
        System.out.println(student.name);//null
        System.out.println(student.age);//0
        student.name = "zhangsan";
        student.age = 10;
        System.out.println(student.name);//zhangsan
        System.out.println(student.age);//10
        student.study();//zhangsan在学习
        student.name = "lisi";
        student.study();//lisi在学习
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值