java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java类和对象怎么定义

Java类和对象怎么定义?新手必看的类与对象创建教程

作者:haozigegie

还在为Java类和对象发愁?本文用最接地气的方式讲透类定义、对象创建、内存分配、public/private/protected封装区别,以及构造方法和this关键字的使用,帮你快速掌握Java面向对象基础

对象和类

类的定义

使用类创建对象

创建对象

 创建对象的内存分配

实例

/*

定义类,一个Java程序可以定义多个类,但只能定义一个与程序名称相同的公有类
[修饰符] Class 类名{
	
	数据成员;
	.
	.
	.
	方法成员;
	.
	.
	.
	
}
 
*/
class Student{
	int age;
	String name;
	char gender;
	double score;
	
	void msgPrintf(){
		System.out.println("name is " + name);
		System.out.println("age is " + age);
		System.out.println("gender is " + gender);
		System.out.println("score is " + score);
	}
} 


public class Demo1 {
	public static void main(String[] args) {
		
		//创建对象
		//方式1:先声明再使用new关键字创建
		Student s1;  //声明某个对象属于某个类:类名 对象名;
		s1 = new Student();  //使用new关键字创建对象
		
		s1.age = 20;
		s1.name = "jiangxiaoya";
		s1.gender = '女';
		s1.score = 99.9;
		s1.msgPrintf();
		System.out.println();
		
		//方式2:声明对象的同时并使用new关键字创建对象
		Student s2 = new Student();
		s2.age = 21;
		s2.name = "haozige";
		s2.gender = '男';
		s2.score = 66.6;
		s2.msgPrintf();
		System.out.println();
		
		//方式3:不声明对象,直接使用new关键字,该方式无法向方法传参
		new Student().age = 18;
		new Student().name = "zhutoucai";
		new Student().gender = '女';
		new Student().score = 88.8;
		new Student().msgPrintf();
	}	
}

类的成员封装

public公有封装

private私有封装

protected保护封装

不进行封装

实例

class Student{
	//private关键字,只有该类才能访问,私有成员
	//public关键字,该类或非该类均可以访问,公有成员
	//protected关键字,该类及其子类的成员可以访问,同一个包中的类也可以访问
	//默认,同一个包中的类可以访问
	
	private int age;  //私有成员
	protected String name;  //保护成员
	char gender;
	double score;
	
	void msgPrintf(){
		System.out.println("name is " + name);
		System.out.println("age is " + age);
		System.out.println("gender is " + gender);
		System.out.println("score is " + score);
	}
	
	void inputAge(){
		age = 21;
	}
} 


public class Demo1 {
	public static void main(String[] args) {
		
		Student s2 = new Student();
		//s2.age = 21;  由于Student类中的数据成员age是私有成员,因此不能直接访问它
		s2.inputAge();  //间接访问私有成员
		s2.name = "haozige";  //同一个包中的类也能访问保护成员
		s2.gender = '男';
		s2.score = 66.6;
		s2.msgPrintf();
		System.out.println();
		

	}	
}

构造方法

构造方法主要用于在使用new关键字创建对象时同时传递参数进行数据成员的初始化

定义构造方法

构造方法在定义类中定义
类名称(参数列表){
       赋值语句;
}

使用构造方法

类名称 对象名 = new 类名称(参数列表)

实例

class Student{
	private int age;  //即使是私有成员,在创建对象时一样能进行初始化
	String name;
	char gender;
	double score;
	
	void msgPrintf(){
		System.out.println("name is " + name);
		System.out.println("age is " + age);
		System.out.println("gender is " + gender);
		System.out.println("score is " + score);
	}
	
	//构造方法1
	Student(int newage,String newname,char newgender,double newscore){
		age = newage;
		name = newname;
		gender = newgender;
		score = newscore;
	}
	
	//构造方法一样可以进行方法重载
	Student(int newage,String newname){
		age = newage;
		name = newname;

	}
}

public class Demo3 {
	public static void main(String[] args) {
		
		//创建对象时想同时传递参数进行数据成员的初始化时可以使用构造方法
		Student s1 = new Student(21,"haozige",'男',66.6);  //对应定义类Student的构造方法1
		s1.msgPrintf();
		System.out.println();
		
		Student s2 = new Student(20,"jiangxiaoya");  //对应定义类Student的构造方法2
		s2.msgPrintf();
	}
}

this关键字

实例

class Student{
	int age;
	String name;
	int sex;
	double sorce;
	
	//构造方法1
	Student(){
		System.out.println("我是构造方法1");
	}
	
	//构造方法2(方法重写)
	Student(int age,String name,int sex,double sorce){
		this();  //在构造方法中使用this关键字调用其他构造方法
		
		//this关键字解决形参同名的问题
		this.age = age;
		this.name = name;
		this.sex = sex;
		this.sorce = sorce;
	}

	void msgPrintf(){
		System.out.println(this.age);  //this关键字调用本类的数据成员或者方法
	}
	
	void myPrintf(){
		
		this.msgPrintf();  //this关键字调用本类的数据成员或者方法
		
		//this关键字看作一个变量,值为为调用此方法的对象
		Student stutmp = this; 
		System.out.println(stutmp.name);
		
	}
}

public class Text {
	public static void main(String[] args) {
		Student stu1 = new Student(20,"jiangxiaoya",0,99.5);
		
		stu1.myPrintf();
		
	}
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文