java 面向对象(类和对象)

类和对象的概念

  • 类是对象的描述,对象是类的个体;类是抽象的,具有相同属性和方法的对象的集合;确定对象将会有的属性和方法;
  • 对象:用来描述客观事物的一个实体,有一组属性和方法组成.

类与对象的关系;

类是对象的描述,对象是类的个体;类是抽象的,对象是具体的;

成员变量和局部变量

成员变量和局部变量
变量申明的位置决定变量的作用域
局部变量只能在局部使用;
局部
成员变量:构造类对象是=时产生;
局部变量:
为什么需要包:
- 文档分门别类
- 易于管理
不同文档放在不同的包中,易于区分.
package com.jredu.
包名由小写字母组成,不能以原点开头和结尾
包名之前最好加上唯一的前缀,通常使用组织倒置时的网址.
*代表包中所有的类;

面向对象编程的基本步骤

1.构造类;
2.构造对象;
3.使用对象;

编写一个类的步骤

  • 定义类
  • 定义属性
  • 定义方法

java中的类的定义

类 [访问控制符]class 类名{}
属性 [访问控制符] 数据类型 变量名;
方法 [访问控制符] void返回值类型 方法名;

public  Student (){

}   //类
public void Student(){
}   //定义方法

由类产生对象(对象的实例化)

类名 对象名 = new 类名();

Student student = new Student();

构造函数

构造函数
写或不写都不存在;
由类产生对象的时候会调用它(实例化对象的时候会调用构造函数)

public Student(){
    int score;  //类中的属性
    public Student(){ //无参构造函数
        System.out.println("对象实例化的时候调用了我!")
    }
    public Student(int score){ //有参构造函数
        this.score = score;  //实现传参
    }
}

有无返回值

传递整型数据

public Student(){
    int score;  //类中的属性
    }
    public void show(int score){ //无返回值
        this.score = score; 
        System.out.println("该同学的成绩是"+score);

    }
    public int sum(int score){ //有返回值  返回值类型为整型
        this.score = score; 
        sum+=score;
        return sum;

    }
}

传递字符串

public Student(){
    String name;  //类中的属性
    }
    public String sum(String name){ //有返回值  返回值类型为字符串型
        this.name = name; 
        return name;
    }
}

传递数组

//定义类
public class ScoreCalc {
    public int sum(int s[]){
        int sum = 0;
        for(int i=0;i<3;i++){
            sum += s[i];
        }
        return sum;//返回int类型的数据
    }
//调用
public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int [] s = new int[3]; 
        for(int i = 0;i<3;i++){
            System.out.println("请输入第"+(i+1)+"位同学的成绩");
            s[i] = input.nextInt();
        }
        ScoreCalc score = new ScoreCalc();
        int sum = score.sum(s);

传递对象数组;

//  定义 Student类
public class Student {
    int height;
    public Student (){
    }
    public Student(int height){
        this.height = height;
    }
}
//  StudentCalc类
public class StudentCalc {
    public double calc(Student [] s) {  //传递Student类对象类型的数组
        int sum = 0;
        for(int i=0;i<s.length;i++){
            sum+=s[i].height;  //使用类中的属性
        }
        return sum/(s.length); //返回值为double型
    }

}
//测试类
public class StudentTest {  //定义测试类
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Student student[] = new Student[5];  //实例化Student类的数组对象;
        for(int i = 0;i<5;i++){
            System.out.println("请输入第"+(i+1)+"位同学的身高(cm)");
            int height = input.nextInt();
            student[i]=new Student();  //实例每个数组对象内的元素
            student[i].height = height;//传参
        }
        StudentCalc calc = new StudentCalc();  //实例化StudentCalc对象
        double avg = calc.calc(student);//调用的同时传递参数   Student类的数组
        System.out.println("平均身高是"+avg+"cm");
    }

}

总结:

类和对象的的方法大大提高了代码的复用性,有利于团队协作;减少重复代码量;
将方法提取到类的过程称为封装;类就像一个黑箱子,你可以不用知道其内部的具体属性,你只需要知道类中有那些属性和方法,然后调用就可以了.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值