day12

package com.oop.demo03;

public class Pet {
String name;
int age;
public void shout(){
System.out.println("叫了一声");
}
}


package com.oop.demo03;

import com.oop.demo03.Pet;
/*
1、类与对象
类是一个模板:抽象,对象是一个具体的实例
2、方法
定义,调用
3、对象的引用
引用类型:基本类型(8),对象是通过引用来操作的:栈---->堆
4、属性:字段Field 成员变量
默认初始化:
数字:0
char:u0000
bolean:false
引用:null
属性定义:修饰符 属性类型 属性名 = 属性值!
5、对象的创建和使用
-必须使用New关键字创建对象,构造器 Person lisa= new Person();
-对象的属性 lisa.name
-对象的方法 lisa.sleep();
6、类:
静态的属性 属性
动态的行为 方法
封装、继承、多态
*/
public class Application {
public static void main(String[] args) {
Pet dog=new Pet();
dog.name="旺财";
dog.age=3;
dog.shout();
System.out.println(dog.name);
System.out.println(dog.age);
Pet cat=new Pet();
}
}


package com.oop.demo04;
/*
1、提供程序的安全性,保护数据
2、隐藏代码的实现细节
3、统一接口
4、系统可维护性增加了
*/
public class Student {
//属性私有
//名字
private String name;
//学号
private int id;
//性别
private char sex;
//年龄
private int age;

//提供一些可以操作这个属性的方法
//提供public的get、set方法
//alt+insert 自动生成get和set方法

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public char getSex() {
return sex;
}

public void setSex(char sex) {
this.sex = sex;
}

public int getAge() {
return age;
}
public void setAge(int age) {
if(age>120||age<0){//不合法的数据
this.age=3;
}
else{
this.age = age;
}
}
//get获取这个数据

//set给这个数据设置值

//学习()

//睡觉()
}


package com.oop;

import com.oop.demo04.Student;

public class Application {
public static void main(String[] args) {
Student s1=new Student();
s1.setName("lisa");
System.out.println(s1.getName());
s1.setAge(150);//不合法的数据
System.out.println(s1.getAge());
}
}


package com.oop.demo05;
//在java中所有的类都直接或者间接默认继承object
//人 父类
public class Person {
//public 公有的
//protected 受保护的
//default 默认的
//private 私有的
private int money=10_0000_0000;

public int getMoney() {
return money;
}

public void setMoney(int money) {
this.money = money;
}

public void say(){
System.out.println("说了一句话");
}
}


package com.oop.demo05;
//学生 is 人 子类
//子类继承了父类就会拥有父类的全部方法,私有的方法以及成员变量无法继承
public class Student extends Person{

}


package com.oop.demo05;

import com.oop.demo05.Student;

public class Application {
public static void main(String[] args) {

Student student=new Student();
student.say();
System.out.println(student.getMoney());
}
}



posted @ 2022-05-02 18:08  1号小白学编程  阅读(58)  评论(0编辑  收藏  举报