面向对象
类的定义
/**
[修饰符]class 类名{
//成员变量
//构造方法
//成员方法
}
*/
成员变量
/**
[修饰符]变量类型 变量名字 [= 默认值] ;
其中"修饰符"和"默认值"不是必须的。
修饰符有:
[public | protexted | private]
[static](static只执行一次)
[final]
定义在类里面
*/
int A ;
int B = 10 ;
public int C ;
protected int D ;
private int E ;
static int F ;
final int G ;
定义成员变量
public class Demo {
int A = 10; //成员变量 A
public static void main(String[] args) {
Demo Demo = new Demo();
}
public Demo() { //构造方法Demo
System.out.println("Hello,World!");
}
}
构造方法
和类名相同,自动调用
public class Demo {
public static void main(String[] args) {
Demo Demo = new Demo();
}
public Demo() { //构造方法Demo
System.out.println("Hello,World!");
}
public Demo(int R,int E){
System.out.println("使用两个参数的构造方法");
}
}
成员方法
/**
[修饰符]返回值类型 方法名{
语句
}
*/
public int NP(int G){
return G;
}
构造方法重载
两个方法方法名相同,但是参数列表不同,称为重载
public Demo(double U){
System.out.println("使用构造方法");
}
public Demo(int Y){
System.out.println("使用构造方法重载");
}
调用以上方法
public class PPP {//新建PPP的类
public static void main(String[] args) {
AAA N = new AAA(1.2);//将带有成员方法和构造方法的类实例化
System.out.println("成员方法" + N.NP(8)); //成员方法调用
AAA N1 = new AAA(1);//调用
AAA N2 = new AAA(7,4);
}
}
public class AAA {
public int NP(int G){ //成员方法
return G;
}
public AAA(double U){ //构造方法
System.out.println("使用构造方法");
}
public AAA(int Y){ //构造方法
System.out.println("使用构造方法重载");
}
public AAA(int R,int E){ //构造方法
System.out.println("使用两个参数的构造方法");
}
}
输出结果:
This 关键字
在类的方法中引用自身
Student类代码
public class Student {
String name = "";
int age ;
double Math ;
double Chinese ;
double English ;
public Student(String name,int age,int English,int Math,int Chinese){
this.name = name;
this.age = age ;
this.English = English ;
this.Math = Math ;
this.Chinese = Chinese ;
}
public void printStudentInfo(){
System.out.println("姓名:" + this.name + " 年龄:"+ this.age + " 各科成绩:" + "语文:" + this.Chinese + "数学:" + this.Math + "英语:" + this.English );
}
public void printStudentInfo(String E , int A){
System.out.println("姓名:" + name + " 年龄:"+ age);
}
}
调用Student代码
public class TestStudent {
public static void main(String[] args) {
Student Student1 = new Student("",0,0,0,0);
Student1.name = "哈吉";
Student1.age = 12;
Student1.English = 123;
Student1.Math = 123;
Student1.Chinese=123;
Student1.printStudentInfo("123",123);
Student1.printStudentInfo();
System.out.println("===================");
Student Student2 = new Student("张天",12,92,86,87);
Student2.printStudentInfo();
Student2.printStudentInfo("张天",12);
}
}
方法的参数传递
当初参数是基本数据类型的时候,经过方法输出后参数不变,如果是引用数据类型,会改变
//引用数据类型当参数
public class Demo {
public static void main(String[] args) {
int[] AA = {50,100};
Data(AA);
System.out.println("AA[0]= " + AA[0] + ", AA[1]= " + AA[1]);
}
public static void Data(int[] BB){
int AP = BB[0];
BB[0] = BB[1];
BB[1] = AP;
System.out.println("BB[0]= "+ BB[0] + " ,BB[1]= " + BB[1]);
}
}
输出结果:
//基本数据类型当参数
public class Demo {
public static void main(String[] args) {
int a = 100;
int b = 50;
Data(a,b);
System.out.println("a= " + a + ", b= " + b);
}
public static void Data(int x,int y){
int AP = x;
x = y;
y = AP;
System.out.println("x= "+ x + " ,y= " + y);
}
}
输出结果:
可变长参数
public class GongSi { //输出
public void BuMen (String A,String...B){ //String... 为可变长参数,需要遍历输出,String为数据类型
System.out.println("部门为 " + A);
for (int i =0; i <B.length;i++){
System.out.println("人员为 " + B[i]);
}
}
}
public class Demo { // 赋值输出
public static void main(String[] args) {
GongSi GongSi = new GongSi();
GongSi.BuMen("123不mean","123123123人","123123啊的时间弄多久哦爱仕达");
}
}
类(Class)使用Public修饰,说明给构造方法名需要于类名相同
public定义的表示在此程序下(同一项目下)都可以被访问和调用