JAVA基础day4

对象和类

对象(Object / instance) :里面包含多种变量和方法。
类(class):相当于对象的摸板。

类的定义


public class Student {   //一个.java文件只能有一个public修饰的类

	//属性fields(成员变量)
	int id;
	String name;
	int age;
	Computer comp; //计算机
	
	//方法
	void study(){
		System.out.println("我在认真学习!!使用电脑品牌:" + comp.brand);
	}
	
	void play(){
		System.out.println("多人运动吗?");
	}
	
	//程序执行入口,必须要有
	public static void main(String[] args){
		Student stu = new Student();  //创建一个对象。Student(){} 是构造方法,用于创建这个类的对象。无参的构造方法可以由系统自动创建。
		stu.id = 1001;
		stu.name = "fehling";
		stu.age = 18;

		Computer c1 = new Computer();
		c1.brand = "联想";
		stu.comp = c1;
		
		stu.play();
		stu.study();
	}
	
}
	
class Computer {
	String brand;
}
内存分析

Java虚拟机的内存分为三个区域:栈stack、堆heap、方法区method area。
栈:是方法的内存模型,每个方法被调用都会创建一个栈帧。
堆:用于存储创建好的对象和数组(数组也是对象)。
方法区:实际也是堆,用于存储 类相关的信息、静态变量、字符串常量等不变或唯一的内容。在这里插入图片描述

构造方法(constructor)

构造方法用于对象初始化。
1、通过new 关键字调用;
2、构造其虽然有返回值,但是不能定义返回值类型,不能在构造器里使用return返回某个值;
3、如果没有定义构造器,则编译器会自动定义一个无参数的构造函数;如果已定义则编译器不会自动添加;
4、构造器的方法名必须和类名一致。

class Point{
	double x, y;

	//构造方法名称和类名称必须保持一致
	public Point(double _x, double_y){
		x = _x;
		y = _y;
		return;
	}

	public double getDistance(Point p){
		return Math.sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
	}

}

public class TestConstructor{
	public static void main(String[] args){
		Point p = new Point(3.0, 4.0);
		Point origin = new Point(0.0, 0.0);
		
		System.out.println(p.getDistance(origin));
	}
}
this关键字

this不能用于static方法中。

public class Test{
	int a, b, c;

	Test(int a, int b){
		this.a = a;
		this.b = b;  //this指明当前对象,给上面的成员变量a,b赋值

	Test(int a, int b, int c){
		this(a, b);  //调用上面的Test构造方法,必须位于第一行
		this.c = c;
	}

	void sing (){
	}

	void eat(){
		this.sing();  //调用本类中的sing();
		System.out.println("....");
	}

	public static void main(String[] args){
		Test hi = new Test(2, 3);
		hi.eat();
	}
}
static关键字

在类中,用static修饰过的成员变量和方法从属于类(普通变量和方法从属于对象),其生命周期和类相同。

public class User{
	int id;
	String name;
	String pwd;

	static String company = "北京尚学堂";

	public User(int id, String name){
		this.id = id;
		this.name = name;
	}

	public void login() {
		printCompany();  //普通方法中可以调用静态方法
		System.out.println(company); //同样可以调用静态变量
		System.out,println("登录" + name);
	}
	
	public static void printCompany(){
//		login();  //静态方法中,不能调用普通方法
		System.out.println(Company); //可调用静态的
	}

	public static void main(String[] args){
		User u = new User(101, "高小七");
		User.printCompany();
		User.company = " 北京阿里爷爷";
		User.printCompany();
	}
}

在这里插入图片描述

静态初始化块
public class User{
	int id;
	String name;
	static String company;

	static {
		System.out.println("执行类的初始化工作");
		company = "阿里爷爷";
		printCompany();
	} //静态初始化块,对类初始化
	
	public static void printCompany(){
		System.out.println(company);
	}
	public static void main(String[] args){
		User u = null;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值