1、五、类的概念(class)类是一组具有相同属性和行为的对象的抽象。第二节 类及对象的创建一、类的定义类是数据和函数的集合。类是一种类型。类是Java语言基本元素,类由成员变量和方法组成;类定义的一般形式如下:class 类名 变量(常量)定义构造方法定义方法定义其中访问权限为 public、private、protected或取默认值。 public class StudentTest public static void main(String args) Student studentA = new Student(); Student studentB = new Student();
2、studentA.setId(20001); studentA.setName(张三 studentA.setChinese(90); studentA.setMath(80); studentB.setId( studentB.setName( studentB.setChinese(90); studentB.setMath(85); System.out.println(studentA.Sum(); System.out.println(studentB.Sum();public class Student private String id; private String name;
3、 private int math; private int chinese; public void setId(String id) this.id = id; public void setName(String name) this.name = name; public void setMath(int math) this.math = math; public void setChinese(int chinese) this.chinese = chinese; public int Sum() return math + chinese;例题2: public String
4、getId() return id; public String getName() return name; public int getMath() return math; public int getChinese() return chinese; return getMath() + getChinese();20002 System.out.println(name:+studentA.getName()+tId:+studentA.getId()+tscore:+studentA.Sum();+studentB.getName()+studentB.getId()+tscore
5、+studentB.Sum();studentA1111li9080StudentB20002李四结合上例讲解什么是类、数据成员、方法二、上例的执行流程从主方法开始执行对上例的改进 public String getStudentInfo() return +getName()+getId()+Sum();* System.out.println(studentA.getStudentInfo();改进2: public String toString() System.out.println(studentA); System.out.println(studentB);三、创建对象对象是一
6、种复合数据。由类产生。1.定义对象定义对象的格式为: 类名 对象名如:Student st;2.建立对象建立对象使用NEW关键字。(1)为该对象分配内存空间;(2)返回该内存空间的引用,返回首地址;(引用是指指针的别名)(3)调用构造函数,构造函数自动将成员函数赋初值。st=new Student();st包括新建对象的“引用”-地址。注:可将上两步合二为一Student st=new Student();四、生成临时对象public class StudentTest public static void main(String args) new Student().setAge(Li c
7、lass Student private int age; void setName(String s) name=s; public String getName() return name; void setAge(int a) age=a; public int getAge() return age;五、成员的调用一般情况下,我们只能通过对象访问公有成员(public)保护成员(protected),不能通过对象访问私有成员(private)具体方法是:对象名.成员名 String name; int age;public class StudentTest2 /* Creates a
8、 new instance of StudentTest2 */ public static void main(String a) Student student=new Student(); student.name=li; student.age=30; System.out.println(student.name); System.out.println(student.age);六、 null 关键字 七、为什么要使用类和对象对象用来处理复合数据。但由于其复杂性不能一一定义,所以由类生成。当然还有封装、继承、多态。八、类什么时候被使用类用来生成对象和被继承九、类分为哪几种自定义 类
9、标准类库第三方类库十、简单方法的命名规则一般通过set设置数据成员(常量和变量)的值一般通过get获取数据成员(常量和变量)的值十一、面向对象的特性抽象、封装、继承、多态实例:public class Triangle double sideA, sideB, sideC; double area; boolean triangle; public void setSideA(double a) sideA = a; public void setSideB(double b) sideB = b; public void setSideC(double c) sideC = c; publi
10、c double getArea() double p = (sideA + sideB + sideC) / 2.0; area = Math.sqrt(p * (p - sideA) * (p - sideB) * (p - sideC); return area; public boolean isTriangle() if (sideA sideB + sideC & sideB sideA + sideC & sideC sideA + sideB) triangle = true; else triangle = false; return triangle; import jav
11、a.util.Scanner;public class TriangleTest Triangle triangle = new Triangle(); Scanner scanner = new Scanner(System.in);请输入三角形第一边 int a = scanner.nextInt();请输入三角形第二边 int b = scanner.nextInt();请输入三角形第三边 int c = scanner.nextInt(); triangle.setSideA(a); triangle.setSideB(b); triangle.setSideC(c); if (tri
12、angle.isTriangle() System.out.println(triangle.getArea();此三边不能构成三角形第二节 构造方法(Constructor)一、构造方法(Constructor) 1构造方法无返回值,前面也不加void 2、和类的其它方法一样,构造方法有名称、参数和方法体。 3、构造方法名与类同名。 4、使用new来创建对象时,系统会自动调用相应类的构造方法。构造方法的主要作用是:是在创建一个对象时,对这个对象进行初始化。例:1、无参数构造方法 public Student() id = 20074071123 this.name = zhang this.math =90; this.chinese = 100;说明:无参构造方法,相对用的比较少。2、有参数的构造方法 public Student(String id, String name, int math, int chinese) * Student studentA = new Student(20084071110,78,90);其他实例:教材实例 :/ EmpInfoC.javapublic class
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1