ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java笔记——类和对象

2021-01-18 18:02:05  阅读:211  来源: 互联网

标签:Java String 对象 sno 笔记 sex Student public name


类和对象

  1. 类的定义
class Student{
	public String sno;  //成员属性
	public String name;
	public String sex;
	
	public void learn() {  //成员方法
		System.out.println("学习");
	}
	public void sleep() {
		System.out.println("睡觉");
	}
}
  • class为定义类的关键字;
  • Student 为类名;
  • { … } 中为类的主体。
  1. 类的实例化
    拿上述的Student例子来看:
public class Main{
public static void main(String[] args) {
		Student stu = new Student();
		stu.learn();
		stu.sleep();
		Student stu1 = new Student();
		Student stu2 = new Student();
	}
}

结果:
在这里插入图片描述

从示例可以看出:

  • new关键字用于创建一个对象的实例
  • 用 “ . ” 来访问对象中的属性和方法
  • 同一个类可以创建多个实例
  1. 方法
    对于上述Student来说,learn是一个方法,表示Student 这个对象具有“学习”的行为;
    同样,sleep也是一个方法,代表Student对象具有“睡觉”的行为。

构造方法

在实例化对象的时候挥别自动调用到的方法,方法名字和类名相同,用于对象的初始化。

class Student{
	public String sno;  //成员属性
	public String name;
	public String sex;
	
	    Student(String sno,String name, String sex){  //构造方法
        this.sno = sno;
        this.name = name;
        this.sex = sex;
    }
}

public class Main{
    public static void main(String[] args) {
        Student stu = new Student("2021","小红","女");
        System.out.println(stu.name);
    }
}

结果:
在这里插入图片描述

static关键字

  • 修饰属性
    java静态属性和类相关,和具体的实例无关,即同一个类的不同实例公用同一个静态属性。

代码示例:

class Test{
	public int a;
	public static int count;
}

public class Main{
	public static void main(String[] args) {
	Test t1 = new Test();
	t1.a++;
	Test.count++;
	System.out.println(t1.a);
	System.out.println(Test.count);
	}
}

结果:
在这里插入图片描述
由上述示例可知,count被static所修饰,所有类共享,且不属于对象,访问方式为:类名.属性

  • 修饰方法
    在任何方法上应用static关键字,称为静态方法

  • 代码块
    使用{ }定义的一段代码
    可分为:

普通代码块

public class Main{
    public static void main(String[] args) {
		//普通代码块
        {
            int x = 1;
            System.out.println("x1 = " + x);
        }
        
        int x = 100;
        System.out.println("x2 = " + x);
    }
}

结果:
在这里插入图片描述

构造块

class Student{
    public String sno;  //成员属性
    public String name;
    public String sex;

    Student(){
        System.out.println("i am a student");
    }

    //实例代码块
    {
        this.sno = "20210201";
        this.name = "张三";
        this.sex = "男";
    }
    public void show(){
        System.out.println("sno:" + sno + " name: " + name + " sex: " + sex);
    }
}

public class Main{
    public static void main(String[] args) {
        Student stu = new Student();
        stu.show();

    }
}

结果:
在这里插入图片描述

静态块

class Student{
    public String sno;  //成员属性
    public String name;
    public String sex;

    Student(){
        System.out.println("i am a student");
    }
    
    //静态代码块
    static {
        System.out.println("i am learning");
    }
}

public class Main{
    public static void main(String[] args) {
        Student stu = new Student();
    }
}

结果:
在这里插入图片描述

同步代码块

  • 修饰类
  1. this指针
  • this引用的类型:对应类类型引用,即那个对象调用就是那个对象的引用类型
  • this引用只能在"成员方法中使用"
  • this引用具有final属性,在一个成员方法中,不能再去引用其他的对象
  • this引用是成员方法第一个隐藏的参数,编译器会自动传递,在成员方法执行时,编译器会负责将
    调用成员方法对象的引用传递给该成员方法,this引用复杂来接收
  • 在成员函数中,所有成员变量的访问,都会被编译器修改通过this来访问

示例:通过this调用自身的构造方法

class Student{
    public String sno;  //成员属性
    public String name;
    public String sex;

    Student(String sno,String name, String sex){
        this.sno = sno;
        this.name = name;
        this.sex = sex;
    }
    public void show(){
        System.out.println(this.name+"的学号是"+this.sno);
    }
}

public class Main{
    public static void main(String[] args) {
        Student stu = new Student("2021","小红","女");
        stu.show();
    }
}

结果:
在这里插入图片描述

标签:Java,String,对象,sno,笔记,sex,Student,public,name
来源: https://blog.csdn.net/m0_46959770/article/details/112788338

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有