第四章 类与对象(自用)

这章我觉得 用实验来巩固知识点比较好。

目录:

1.  类的定义

2.  对象的创建与使用

3.  参数的传递


实验目的:

<1>学习类的一般结构与类的定义

<2>学习类的成员变量和成员方法的声明

package 类与对象;
public class Student {
        int chinese,math,english;
        int  total(){
           return chinese+math+english;
        }
        int average(){
           return total()/3;
        }
        void setEle(int a,int b,int c){
            chinese=a;
            math=b;
            english=c;
        }
}
package 类与对象;

import  java.util.*;
import  java.io.*;
public class test1 {
    public static void main(String[] args) {
        Student s1=new Student();
        s1.chinese=108;
        s1.math=124;
        s1.english=148;
        Student s2=new Student();
        s2.chinese=100;
        s2.math=150;
        s2.english=149;
        System.out.println(s1.total()+","+s1.average());
        System.out.println(s2.total()+","+s2.average());
        Student s3=new Student();
        s3.setEle(100,120,140);
        System.out.println(s3.total()+","+s3.average());
    }
}


对象直接new 

参数直接传 

这两个不用针对写实验

猜你喜欢

转载自blog.csdn.net/qq_41048982/article/details/108421722
今日推荐