2021_08_18学习日志

本文深入探讨了面向对象编程的核心概念,包括类与对象、封装、继承和多态。通过示例代码展示了静态与非静态方法的使用,以及值传递和引用传递的区别。此外,还讲解了方法定义、命名规则、异常处理和方法调用。通过实例解释了类与对象的关系,强调了在实际编程中如何创建和操作对象。
摘要由CSDN通过智能技术生成

面向对象编程(OOP)

(Object-Oriented Programming)

以类的方式组织代码,以对象的方式组织(封装)数据

  • 封装
  • 继承
  • 多态

回顾方法

方法定义

  • break和return的区别

    return代表方法结束

  • 方法名的命名规则,小驼峰,见名知意。

  • 参数列表:参数类型 参数名

  • 异常抛出:(后面补充)

方法的调用

  • 静态方法

  • 非静态方法

    package com.oop.demo01;
    
    public class demo02 {
        public static void main(String[] args) {
            Student.say();
            System.out.println("===============================");
            Student student = new Student();//非静态要实例化
            student.talk();
        }
    
        public static void a(){
            //b(); static和类一起加载的,而非静态是类实例化之后才存在。所以不能直接调用b();
        }
        public void b(){
        }
    }
    
package com.oop.demo01;

public class Student {
    //静态方法
    public static void say(){
        System.out.println("say something");
    }
    //非静态方法
    public void talk(){
        System.out.println("非静态");
    }
}

值传递

package com.oop.demo01;

//值传递
public class Demo04 {
    public static void main(String[] args) {
        int a = 1;
        System.out.println(a);
        Demo04.change(a);
        System.out.println(a);//输出之后还是原来的值
    }
    public static void change(int val){
        val = 10; //将输入变成10
    }
}

引用传递

package com.oop.demo01;

//引用传递:对象,本质还是值传递
public class Demo05 {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.name); //null
        Demo05.change(person);
        System.out.println(person.name); //axin
    }
    public static void change(Person person){
        person.name = "axin"; //指向具体的对象,可以改变属性
    }
}
//定义了一个类,有一个属性叫name
class Person{
    String name;
}

类与对象的关系

类数抽象的数据类型,表示一类实物的整体描述

对象是抽象概念的具体事例

public class Student {
    //属性
    String name;
    int age;
    //方法
    public void study(){
        System.out.println(this.name + "在学习");
    }
}
public class Application {
    public static void main(String[] args) {
        //实例化抽象类
        Student student = new  Student();
        Student student1 = new Student();

        student.name = "XiaoMing";
        student.age = 13;
        student1.name = "XiaoHong";
        student1.age = 14;

        System.out.println(student.age);
        System.out.println(student1.name);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

写代码的信哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值