初识类与对象:
小总结:(类可以看做是一个模版,或者图纸,系统根据类的定义来造出对象。)
1.对象是具体的事物;类是对对象的抽象。
2.类可以看成一类对象的模板,对象可以看成该类的一个具体实例。
3.类是用于描述同一类型的对象的一个抽象概念,类中定义了这一类对象所应具有的共同的属性、方法(具有相同属性和方法的一组对象的集合)。
思考方式:遇到复杂的问题,先从问题中找名词,然后再确立那些可以作为类,再根据问题需求确定类的属性和方法。
一.类:
定义内容:属性,方法,构造器。
范式:属性类型 属性名 =[默认值];(先写属性,分号结束,暂时没值,会对应类型默认值)。
eg:class 类名{
String name;//属性类型 属性名;
int age;
String sex;
void 方法名(){//方法
System.out.println("好好学习,学无止境");
}
}
二.对象:
对象:类的实例,存在一致性(相同属性)和差异性(属性值不同)。
(比如:你叫张三,他叫王五,但都是名字)
范式:类型 对象名=new 类名();
eg: Person per =new Person();//类型:人类 new 创建对象
调用:对象名.属性名;
eg: per.name;
取值:类型 变量名 =对象名.属性名。
eg:String name=per.name;
赋值:对象名.属性名=属性值。
eg: int names="张三";
per.name=names;
三.方法:
eg: void()方法名{
}
调用方法:对象名.方法名(实参列表);
person.eatfood(参数列表);//此处无参
类图:
类名 |
---|
属性 :类型 属性名 +为公共的public,-为私有的 private |
方法 :方法名 |
四.练习:
1.人类:
package ClsText;
public class Person {
String name;//属性默认赋值
int age;
String sex;
double height;
double weight;
void eatfood(){//方法
System.out.println(name+"正在吃饭");
}
void sleep(){
System.out.println(name+"正在睡觉");
}
void study(){
System.out.println(name+"正在学习");
}
}
package ClsText;
public class PersonText {
public static void main(String[] args) {
Person person = new Person();//创建对象
person.name="王广宇";//给属性赋值
person.age=22;
person.height=175.0;
person.weight=140.0;
person.sex="男";
person.eatfood();//调用方法
person.sleep();
String name = person.name;//调用赋值
int age = person.age;
double height = person.height;
double weight = person.weight;
String sex = person.sex;
System.out.println("姓名:"+name+"年龄:"+age+"体重:"+weight+"身高:"+height+"性别"+sex);//打印输出
}
}
2.电脑类:
package ClsText;
public class Computer {
String brand;
double price;
String cpuModel;
String color;
void playGream(){
System.out.println("正在使用该电脑打游戏");
}
void study(){
System.out.println("正在使用该电脑学习");
}
}
package ClsText;
public class ComputerText {
public static void main(String[] args) {
Computer computer = new Computer();
String brand = "惠普";
computer.brand= brand;
String color = "黑色";
computer.color= color;
String cpuModel = "H9500";
computer.cpuModel= cpuModel;
double price = 5000.0;
computer.price= price;
System.out.println("该电脑的属性:"+"品牌:"+brand+"颜色:"+color+"cup:"+cpuModel+"价格:"+price);
computer.playGream();
computer.study();
}
}
3.灯泡类:
package ClsText;
public class Bulb {
double price;
String color;
String brand;
void Light(){
System.out.println("我的功能是带来光明");
}
}
package ClsText;
public class BulbText {
public static void main(String[] args) {
Bulb bulb = new Bulb();
String brand = "欧普杰";
bulb.brand= brand;
String color = "透明";
bulb.color= color;
double price = 3.0;
bulb.price= price;
System.out.println("灯泡信息:"+"品牌:"+brand+ "颜色:"+color+ "价格:"+price);
bulb.Light();
}
}
4.杯子类:
package ClsText;
public class Cup {
String color;
double capacity;
double height;
double weight;
double price;
void Drink(){
System.out.println("我的功能是盛水");
}
void fall(){
System.out.println("如果你不喜欢我,可以把我摔了(你有钱的话)");
}
}
package ClsText;
public class CupText {
public static void main(String[] args) {
Cup cup = new Cup();
String color = "白色";
cup.color= color;
double height = 20.0;
cup.height= height;
double weight = 40.0;
cup.weight= weight;
int capacity = 500;
cup.capacity= capacity;
int price = 30;
cup.price= price;
cup.Drink();
cup.fall();
System.out.println("杯子的信息:"+"颜色"+color+"高度:"+height+"宽度:"+weight+"价格:"+price+"容量:"+capacity);
}
}
5.椅子:
package ClsText;
public class Chair {
String materils;
int height;
int NumberOflegs;
void seatDown(){
System.out.println("请坐");
}
}
package ClsText;
public class ChairText {
public static void main(String[] args) {
Chair chair = new Chair();
int height = 50;
chair.height= height;
String materils = "红木";
chair.materils= materils;
int numberOflegs = 4;
chair.NumberOflegs= numberOflegs;
chair.seatDown();
System.out.println("椅子信息:"+"高度:"+height+"材料:"+materils+"腿数:"+numberOflegs);
}
}
6.学生类:
package ClsText;
public class Student {
String name;
int age;
String hobby;
String grade;
void StudentMessage(){
System.out.println("我的名字是:"+name+"年龄是:"+age+"爱好是:"+hobby+"班级是:"+grade);
}
}
package ClsText;
public class StudentText {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
String grade = "c40大世界";
s1.grade= grade;
String grade2 = "c40百老汇";
s2.grade= grade2;
String hobby1 = "打篮球";
s1.hobby= hobby1;
String hobby2 = "打台球";
s2.hobby= hobby2;
int age = 22;
s1.age= age;
int age2 = 21;
s2.age= age2;
String name = "张三";
s1.name= name;
String name2 = "王五";
s2.name= name2;
s1.StudentMessage();
s2.StudentMessage();
}
}
7.教师类:
package ClsText;
public class Teacher {
String name;
int age;
String hobby;
String grade;
String course;
int schoolAge;
void TercherMessage(){
System.out.println("我的名字是:"+name+"年龄是:"+age+"爱好是:"+hobby+"班级是:" +
""+grade+"课程是:"+course+"教龄:"+schoolAge);
}
}
package ClsText;
public class TeacherText {
public static void main(String[] args) {
Teacher t1 = new Teacher();
Teacher T2 = new Teacher();
String grade = "c40大世界";
T2.grade= grade;
String grade2 = "c40百老汇";
t1.grade= grade2;
String hobby1 = "打篮球";
T2.hobby= hobby1;
String hobby2 = "打台球";
t1.hobby= hobby2;
int age = 22;
T2.age= age;
int age2 = 21;
t1.age= age2;
String name = "张三";
T2.name= name;
String name2 = "王五";
t1.name= name2;
String course = "JAVA";
T2.course= course;
String course1 = "python";
t1.course= course1;
int schoolAge = 10;
T2.schoolAge= schoolAge;
int schoolAge1 = 20;
t1.schoolAge= schoolAge1;
T2.TercherMessage();
t1.TercherMessage();
}
}