day05-面向对象给学生类属性赋值并调用学生类方法

This commit is contained in:
2026-07-19 10:19:53 +08:00
parent 2a10068ce9
commit 8799e53040
2 changed files with 47 additions and 1 deletions
+46
View File
@@ -34,6 +34,52 @@ package com.inmind.object01;
开发中:java,c++,js...,遇到功能需求,不是直接开始写代码,而是先看一看有没有已经实现好的功能方法,如果有直接用,如果没有的话,那就得自己写。
java面向对象:大部分是面向对象,直接调用已经写好的功能的,但是还是有一部分是面向过程得自己实现一部分。
--------------------------------------------------------------
面向对象给学生类对象属性赋值并调用学生类方法
类与对象的关系:类是抽象(设计图),对象是具体(根据设计图创建出来真正存在的实体)
一个类的属性变量和方法都要通过该类的对象来操作
如何创建出对象???
创建对象的格式:
类名 对象名 = new 类名();
Student s = new Student();
总结:
要对另一个类的属性变量和行为方法进行调用操作,必须创建该类的对象,并通过该对象来操作:
a.对象.属性变量
b.对象.行为方法(参数列表)
*/
public class Demo01 {
public static void main(String[] args) {
/*
创建一个学生对象
Student:表示创建的对象的类型
s:表示对象名,用来操作对象的,s中保存了真正学生对象的地址
new:在堆内存中创建内容
Student():表示要创建的对象,跟之前的类名保持一致
*/
Student s = new Student();//对象
System.out.println(s);//com.inmind.object01.Student@3b07d329
//属性(取值和赋值)
//属性的取值
System.out.println(s.name);//null
System.out.println(s.age);//0
System.out.println(s.score);//0.0
//属性的赋值
s.name = "张三";
s.age = 18;
s.score = 100.0;
s.gender = "";
s.id = 1;
System.out.println(s.name);//张三
System.out.println(s.age);//18
System.out.println(s.score);//100.0
//行为(调用功能)
s.eat("牛排");
s.sleep();
s.study("java");
}
}