• Java 面向对象(上)


    3.2类与对象

    3.2.1类的定义

    class student{//类名student
            String name;//成员变量
            int age;
            String dex;
            String sex;
            void read(){//成员方法
                System.out.println("大家好,我是"+name+",我在看书!");
            }
        }
    public class java_code {
        public static void main(String[] args)
        {
            student arr = new student();
            arr.name = "remarkable";
            arr.read();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    3.2.2对象的创建与使用

    创建student类的对象

    student stu = new student();
    
    • 1
    class student{//类名student
        String name;//成员变量
        void read(){//成员方法
            System.out.println("大家好,我是"+name+",我在看书!");
        }
    }
    
    public class java_code {
        public static void main(String[] args)
        {
            student stu1 = new student();
            student stu2 = new student();
            stu1.name = "Xiaoming";
            stu1.read();
            stu2.name = "Xiaohua";
            stu2.read();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    3.2.3对象的引用传递

    class student{//类名student
        String name;//成员变量
        int age;
        void read(){//成员方法
            System.out.println("大家好,我是"+name+",年龄:" + age);
        }
    }
    
    public class java_code {
        public static void main(String[] args)
        {
            student stu1 = new student(); //给stu1分配堆内存,存储类
            student stu2 = null;//不会重新开辟新的堆内存
            stu2 = stu1;//将stu1对象的堆内存空间的使用权分配给stu2
            stu1.name = "Xiaoming";
            stu1.age = 18;
            stu2.age = 20;//通过stu2对象去修改stu1对象
            stu1.read();
            stu2.read();
        }
    }
    大家好,我是Xiaoming,年龄:20
    大家好,我是Xiaoming,年龄:20
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    3.2.4访问控制

    1、4种访问权限:

    (1)private:私有访问权限,只能在本类中访问

    (2)default:默认访问权限,只能在本包中访问

    (3)protected:受保护的访问权限

    (4)public:公共访问权限,

    class Test{
        public int aa; //aa可以被所有的类访问
        protected boolean bb;
        void cc(){
            System.out.println("包访问权限");
        }
        //private权限的内部类,即这是私有的内部类,只能在本类中使用
        private class InnerClass{
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    class Test{
        {
            public int aa; //报错,局部变量没有访问权限
            protected boolean bb;
        }
        void cc(){
            System.out.println("包访问权限");
        }
        //private权限的内部类,即这是私有的内部类,只能在本类中使用
        private class InnerClass{
    
        }
    }
    public class dfs {
        public static void main(String[] args)
        {
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    3.3封装性

    3.3.1为什么要封装

    class stu{
        String name;
        int age;
        void read(){
            System.out.println("name:"+name+" 年龄:"+age);
        }
    }
    public class dfs {
        public static void main(String[] args){
            stu st = new stu ();
            st.name = "zhangsan";
            st.age = -18; //-18不符合年龄值,
            //在设计类stu时,应对成员变量的访问做出一些限定,不允许外界随意访问,即就是要满足必要的约束条件
            st.read();
        }
    }
    name:zhangsan 年龄:-18
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    3.3.2如何实现封装

    1、在定义一个类的时候,如果使用private修饰类的属性,被私有化的属性只能在类中被访问,

    如果外界要访问私有属性,则必须通过setter和getter方法设置和获取属性值。

    /*
    *   1.set方法用处
    *       从主函数中获取参数,并对参数加以约束,然后将满足条件的参数赋给类的属性
    *       通过this.属性 来访问类中的属性
    *   2.get方法用处
    *       通过get方法,可以在主函数中访问类中的私有变量
    */
    
    class stu{
        private String name;
        private int age;
        //get方法,获取name和age,对外界提供公有的访问方法
        public String getName(){
            return name;
        }
        public int getAge(){
            return age;
        }
        //set方法,对传入的属性加约束,注意set无返回值
        public void setName(String name){
            this.name = name; //this.name指类中私有变量name, name指本函数中的形参name
        }
        public void setAge(int age){
            if(age <= 0){
                System.out.println("input is waring");
            } else {
                this.age = age;//把满足条件的值赋给类属性name
            }
        }
        void read(){
            System.out.println("name:"+name+" 年龄:"+age);
        }
    }
    public class dfs {
        public static void main(String[] args){
            stu st = new stu ();
            st.setName("zhangsan");//通过set对传入的参数进行卡第一道关卡
            st.setAge(-18);
            st.read();
            // System.out.println(st.name()); 报错,因为name是私有变量
            System.out.println(st.getName()); //通过get方法可以在主函数中得到私有变量
        }
    }
    input is waring
    name:zhangsan 年龄:0
    zhangsan
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    3.4构造方法

    在实例化对象时为对象的属性赋值,可以通过构造方法实现,

    3.4.1定义构造方法

    class stu{
        private String name;
        private int age;
        public stu(){   //无参构造方法
            
        }
        public stu(String name , int age){  //有参构造方法
            this.name = name;
            this.age = age;
        }
         void read(){
            System.out.println("姓名:"+name+" 年龄:"+age);
        }
    }
    
    public class dfs{
        public static void main(String[]  args){
            stu st = new stu("lihua",18);
            st.read();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    3.4.2构造方法的重载

    class stu{
        private String name;
        private int age;
        public stu(){   //无参构造方法
    
        }
        public stu(String name , int age){  //有参构造方法
            this.name = name;
            this.age = age;
        }
        public stu(String name){    //一个参数的构造方法,即重载
            this.name = name;
        }
        public stu(int age){  //一个参数构造方法
            this.age = age;
        }
         void read(){
            System.out.println("姓名:"+name+" 年龄:"+age);
        }
    }
    
    public class dfs{
        public static void main(String[]  args){
            stu st1 = new stu("lihua",18); //两个参数
            stu st2 = new stu("zhongyuchen"); //一个参数name
            stu st3 = new stu(20);   //一个参数age
            stu st4 = new stu();//无参构造方法
            st1.read();
            st2.read();
            st3.read();
            st4.read();
        }
    }
    姓名:lihua 年龄:18
    姓名:zhongyuchen 年龄:0
    姓名:null 年龄:20
    姓名:null 年龄:0
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    3.5 this关键字

    this关键字主要用法有3如下:

    (1)使用this关键字调用本类中的属性

    (2)使用this关键字调用成员方法

    (3)使用this关键字调用本类中的构造方法

    3.5.1调用本类中的属性

    通过this可以访问当前对象的成员

    public stu(int age){  //一个参数构造方法
            this.age = age;
        }
    
    • 1
    • 2
    • 3

    3.5.2调用成员方法

    class stu{
        public void openMouth(){
            
        }
        public void read(){
            this.openMouth(); //当然这里的this可省略
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3.5.3调用本类中的构造方法

    class stu{
        private String name;
        private int age;
        public stu(){   //无参构造方法
            System.out.println("这是一个通过this调用构造方法");
        }
        public stu(String name){    //一个参数的构造方法,即重载
            this(); //调用无参构造方法
            /* 
            *  (1)只能在构造方法中使用this调用其他构造方法,不能在成员方法中使用
            *  (2)在构造方法中,使用this调用构造方法的语句必须放在第一行,且只能出现一次
            *  (3)不能在一个类的两个构造方法中使用this互相调用
            */
            this.name = name;
        }
         void read(){
            System.out.println("姓名:"+name+" 年龄:"+age);
        }
    }
    
    public class dfs{
        public static void main(String[]  args){
            stu st2 = new stu("zhongyuchen"); //一个参数name
            st2.read();
        }
    }
    这是一个通过this调用构造方法
    姓名:zhongyuchen 年龄:0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    3.6代码块

    3.6.1普通代码块

    在方法或语句中定义的大括号

    3.6.2构造块

    又称构造代码块,直接在类中定义的代码块。

    构造块的执行顺序优先于构造方法

    3.7static关键字

    希望某些属性被所有对象共享,就必须将其声明为static属性,随后该属性可以直接使用类名称进行调用。

    3.7.1静态属性

    也称全局属性,访问格式:

    类名.属性名
    stu.name
    
    • 1
    • 2
    class stu{
        private String name;
        private int age;
        static String school = "陕科大";
        public stu(String name){    //一个参数的构造方法,即重载
            this.name = name;
        }
         void read(){
            System.out.println("姓名:"+name+" 年龄:"+age+" 学校:"+school);
        }
    }
    public class dfs{
        public static void main(String[]  args){
            stu st1 = new stu("zhongyuchen");
            stu st2 = new stu("future");
            stu st3 = new stu("remarkable");
            st1.read();
            st2.school = "北航";
            /*
            * 使用static修饰school属性,将其变成公共属性,这样school属性就被stu类的所有对象共享,
            * 只要某个对象的该属性school进行一次修改,则后面的全部学生对象的school属性值都会被改变。
            * static只能修饰成员变量,不能修饰局部变量如函数中的变量。
            */
            st2.read();
            st3.read();
        }
    }
    姓名:zhongyuchen 年龄:0 学校:陕科大
    姓名:future 年龄:0 学校:北航
    姓名:remarkable 年龄:0 学校:北航
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    3.7.2静态方法

    在不创建对象的情况下,通过类名直接调用某个方法,

    因而在方法前加static,就可实现,这叫静态方法。

    访问方式:

    类名.方法
    实例对象名.方法
    stu.read();
    st1.read();
    
    • 1
    • 2
    • 3
    • 4
    class stu{
        private static String name;
        private static int age;
        static String school = "陕科大";
        public stu(String name){   
            this.name = name;
        }
        public static void read(){ //静态方法只能访问静态变量
            System.out.println("姓名:"+name+" 年龄:"+age+" 学校:"+school);
        }
    }
    public class dfs{
        public static void main(String[]  args){
            stu st1 = new stu("zhongyuchen");
    
            st1.read();
            stu.read();
            /*
            * (1)静态方法通过类名直接访问
            * (2)静态方法只能访问静态成员,所以name/age/school为static属性
            * (3)所以在第一次赋name是zhongyuchen后面的所有都是zhongyuchen*/
        }
    }
    姓名:zhongyuchen 年龄:0 学校:陕科大
    姓名:zhongyuchen 年龄:0 学校:陕科大
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    3.7.3静态代码块

    用static修饰的代码块称为静态代码块。

    当类在执行时,类只加载一次,因此静态代码块只执行一次。

    通常使用静态代码块对类的成员变量进行初始化

    package review;
    class Student{
        {
            System.out.println("构造代码块");
        }
        static{
            System.out.println("静态代码块,只执行一次");
        }
        public Student(){
            System.out.println("构造方法");
        }
    }
    public class r1 {
        public static void main(String[] args){
            Student stu1 = new Student();
            Student stu2 = new Student();
            Student stu3 = new Student();
        }
    }
    静态代码块,只执行一次
    构造代码块
    构造方法
    构造代码块
    构造方法
    构造代码块
    构造方法
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    代码块的执行顺序:

    静态代码块 、 构造代码块 、 构造方法

  • 相关阅读:
    442-C++基础语法(111-120)
    【Harmony】在Harmony上面可以使用的Android常用的开源库
    操作系统引论(一)
    图像分割 - 分水岭算法
    java File AbsolutePath的路径不正确
    13 | k8s的部署策略
    全新Storm Core API管理系统源码 免授权版
    气膜建筑膜材分为哪些类型?
    【LeetCode最详尽解答】42-接雨水 Trapping-Rain-Water
    CRM客户管理系统究竟是什么?如何实施?
  • 原文地址:https://blog.csdn.net/QHBfuture/article/details/125546106