1.面向对象
package com.langsin.Method.student;
public class Student {
String name;
String gender;
int age;
String school;
String major;
//构造方法
//构造方法名称与类名保持一致
public Student(){
System.out.println("类默认提供了一个无参的构造方法方便我们创建该类的对象");
}
public void study(int hour) {
System.out.println("这位仁兄学习了" + hour + "小时");
}
public void eat(String food) {
System.out.println("这位仁兄吃了" + food);
}
public void play(String game) {
System.out.println("这位仁兄开心地玩了" + game);
}
}
package com.langsin.Method.student;
public class Demo {
public static void main(String[] args) {
//已经存在了学生类
// 现在要表示一个具体的学生对象
//Scanner scanner=new Scanner(System.in);
//new Student() 调用了一个无参的构造方法,创建了一个学生对象
Student student1 =new Student();
//属性在堆中存放
student1.name="陈铮";
student1.age=12;
student1.gender="男";
student1.school="齐鲁工业大学";
student1.major="人工智能";
System.out.println(student1.name);
System.out.println(student1.age);
System.out.println(student1.gender);
System.out.println(student1.school);
System.out.println(student1.major);
student1.eat("红豆饼");
student1.play("堡垒之夜");
student1.study(6);
Student student2=new Student();
student2.name="刘宇轩";
student2.age=15;
student2.gender="女";
student2.school="齐鲁工业大学";
student2.major="车辆工程";
Student student3=new Student();
student3.name="肖雨涵";
student1.age=14;
student1.gender="女";
student1.school="齐鲁工业大学";
student1.major="地下工程";
}
}
2.类成员的定义
(1)构造方法的定义
[修饰符] 构造器名(形参列表){}
修饰符:public protected private
构造器名:与类名保持一致
不要写返回值类型(void int double.....)
package com.langsin.Method.computer;
import java.util.Arrays;
public class Computer {
String brand;
double price;
String color;
//JAVA会默认提供一个无参的构造方法
// public Computer() {
// }
//构造方法的作用:通过new关键字来调用,完成对象的初始化(创建)操作
//如何区分重名的局部变量和成员变量
//通过this关键字取到成员变量和成员方法
//this指的是正在执行此方法的对象
public Computer(String brand, double price, String color) {
this.brand=brand;
this.color=color;
this.price=price;
}
//方法的重载
//在JAVA中可以定义名称相同但参数列表不同的方法
// public void play(){
//
// }
int[] a = new int[5];
String string = Arrays.toString(a);
public void play(String game) {
}
public void study(int hour) {
}
}
package com.langsin.Method.computer;
public class ComputerDemo {
public static void main(String[] args) {
Computer computer1=new Computer("戴尔",8888.0,"绿色");
System.out.println(computer1.brand);
System.out.println(computer1.color);
System.out.println(computer1.price);
Computer computer2=new Computer("苹果",7777.0,"蓝色");
System.out.println(computer2.brand);
System.out.println(computer2.color);
System.out.println(computer2.price);
}
}