面向对象(this,构造器,封装,实体类)

目录

面向对象注意事项:

this:

构造器

封装

实体类:


面向对象注意事项:

 1、类名建议用英文单词,首字母大写,满足驼峰模式,且要有意义

 2、类中定义的变量也称为成员变量(对象的属性),类中定义的方法也称为成员方法(对象的行为) 
 3、成员变量本身存在默认值,同学们在定义成员变量时一般来说不需要赋初始值(没有意义)。
 4、一个代码文件中,可以写多个class类,但只能一个用public修饰,且public修饰的类名必须成为代码文件名。
 5、对象与对象之间的数据不会相互影响,但多个变量指向同一个对象时就会相互影响了。
        Student s1 = new Student();
        s1.name = "张三";

        Student s2 = new Student();
       s2.name = "李四";
        // 把s1变量中存储的学生对象的地址赋值给了s2变量
        Student s2 = s1;
        s2.name = "李四";
        System.out.println(s1.name);

  6、如果某个对象没有一个变量引用它,则该对象无法被操作了,该对象会成为所谓的垃圾对象。
        s1 = null;
        s2 = null;
        System.out.println(s1.name);

//1.创建一个学生对象,封装波妞的数据
     Student s1 = new Student();
     s1.name = "播妞";
     s1.chinese = 100;
     s1.math = 100;
     s1.printTotalScore();
     s1.printAverageScore();
    
   System.out.println(s1);
    
//Student类:
// 成员变量(对象的属性)
    String name;
    double chinese;
    double math;

// 成员方法(对象的行为)
    public void printTotalScore(){
        System.out.println(name + "的总成绩是:" + (chinese + math));
    }

    public void printAverageScore(){
        System.out.println(name + "的平均成绩是:" + (chinese + math) / 2.0);
    }

2.  s1变量中储存的是对象的地址,因此变量s1也称为引用类型变量

this:

 一个变量,可用在方法中,来拿到当前对象

主要用于解决变量名称冲突问题

构造器

特点:创建对象时,对象会去调用构造器。 Student s = new Student();

应用场景:创建对象时,同时完成对对象成员变量(属性)的初始化赋值。

public class Student{
//构造器
public Student(){
};


//有参构造器
public Student(String name,double score){
}

注:1.类设计时,不写构造器。Java自动生成一个无参构造器

2.一旦定义有参构造器。Java不会生成无参构造器

封装

定义:用类设计对象处理某一个事物的数据时,应该把要处理的数据,以及处理这些数据的方法,设计到一个对象中去。

设计规范:合理隐藏 合理暴露

公开:public   隐藏:private

实体类:

i:成员变量私有,对外提供get,set方法    ——右键——aenerate——Constructor

ii:类中必须要有一个公共无参构造器

特点作用:创建对象,保存数据

public class Student {
   private String name;
   private double score;


    public Student() {
    }

    public Student(String name, double score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

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

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }
}

//类
public class StudentOperator {
    private Student student;
    public  StudentOperator(Student student) {
this.student = student;

    }
}

public void printPass(){
sout
}


public class Test1 {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.setName("波妞");
        s1.setScore(99);
        System.out.println(s1.getScore());
        System.out.println(s1.getName());

StudentOperator operator = new StudentOperator(s1);
operator.printPass();
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值