51,407
社区成员
发帖
与我相关
我的任务
分享
public class 文件名 {
public static void main(String[] args) {
//new Person(); -- 对象,在堆中开辟空间,该空间存放的成员变量
//p -- 引用,存储对象在堆中开辟的内存地址
//Person - 类(属于引用数据类型)
Person p = new Person();
//操作对象:设置属性、获取属性、调用方法
//设置成员变量
p.name = "小龙女";
p.sex = '女';
p.age = 18;
//获取成员变量
System.out.println(p.name);
System.out.println(p.sex);
System.out.println(p.age);
//调用成员方法
p.eat();
p.sleep();
//调用静态方法
Person.method();
}
}
类里面只有属性和方法
1.属性编写在类里、方法外
2.注意:系统会赋默认值
3.分类:
成员变量:属性对象的变量
静态变量:属于类的变量
4.方法:
静态方法:属性类的方法 -- 直接使用类名调用
成员方法:属于对象的方法 -- 使用引用/对象调用
//人类
public class Person {
//属性/全局变量:成员变量和静态变量
//成员变量
String name;
char sex;
int age;
//方法:成员方法和静态方法
//成员方法
public void eat(){
System.out.println(this.name + "吃蜂蜜");
}
public void sleep(){
System.out.println(this.name + "睡觉觉");
}
//静态方法
public static void method(){
System.out.println("Person类的静态方法");
}
}
1.创建对象
2.初始化数据
public class 文件名 {
public static void main(String[] args) {
Person p = new Person();
p.name = "帝将";
p.sex = '男';
p.age = 5000;
p.eat();
p.sleep();
}
}
public class Person {
String name;
char sex;
int age;
//无参数构造:没有参数的构造法
public Person(){
System.out.println("调用了Person的无参构造");
}
//有参构造:有参数的构造方法
public Person(String name,char sex, int age){
this.name = name;
this.sex = sex;
this.age = age;
}
public void eat(){
System.out.println(this.name + "吃饭饭");
}
public void sleep(){
System.out.println(this.name + "睡觉觉");
}
}
public class 文件名 {
public static void main(String[] args) {
Person p = new Person("祝融",'男',4000);
p.eat();
p.sleep();
}
}
1.私有化属性(目的:不能让外界直接访问)
2.添加get(获取)/set(设置)的方法(目的:向外界提供获取、设置的方法,并且可以在方法中设置附加功能)
1.含义:私有的
2.作用:
1.修饰属性:该属性不能在类的外部使用
2.修饰方法:该方法不能在类的外部使用
public class 文件名 {
public static void main(String[] args) {
A a = new A();
a.method02();
}
}
public class A {
private String xxx = "abcdefg";
private void method01(){
System.out.println(this.xxx);
}
public void method02(){
method01();
}
}
public class 文件名 {
public static void main(String[] args) {
User user = new User("123456789","147852",10000);
//user.money = user.money-500;
//System.out.println("余额:" + user.money);
user.setMoney(user.getMoney()-500);
System.out.println("余额:" + user.getMoney());
}
}
import java.time.LocalDateTime;
public class User {
private String username;
private String password;
private double money;
public User(){
}
public User(String username,String password,double money){
this.username = username;
this.password = password;
this.money = money;
}
public void setMoney(double money){
this.money = money;
System.out.println(LocalDateTime.now() + "---" + this.username + "用户设置了money:" + money);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//获取money属性的方法
public double getMoney(){
//做附加功能
System.out.println(LocalDateTime.now() + " -- " + this.username + "用户去获取了money:" + money);
return money;
}
}
1.this.成员变量:调用本对象的成员变量
2.this.成员方法:调用本对象的成员方法
3.this():在构造方法中的第一句调用另一个构造方法
public class 文件名 {
public static void main(String[] args) {
Person p1 = new Person("鸿钧",'男',1000);
Person p2 = new Person("女娲",'女',200);
Person p3 = new Person();
p1.setName("盘古");
System.out.println(p1.getName());
System.out.println(p2.getName());
p1.study();
p3.study();
}
}
public class Person {
private String name;
private char sex;
private int age;
public Person(){
}
public Person(String name,char sex){
this.name = name;
this.sex = sex;
}
public Person(String name,char sex,int age){
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
public void eat(){
System.out.println(this.name + "吃饭饭");
}
public void sleep(){
System.out.println(this.name + "睡觉觉");
}
public void writerCode(){
System.out.println(this.name + "写代码");
}
public void study(){
this.eat();
this.writerCode();
this.sleep();
}
}
1.成员变量
2.无参构造
3.有参构造
4.get/set
5.其他的方法