Java如何设计并编写类

🏷️ 365bet会员登录 📅 2025-06-27 08:26:40 👤 admin 👀 1682 ❤️ 944
Java如何设计并编写类

Unit3如何设计并编写类

一、术语

类就是类型的简称,是抽象的、不具体的、不明确的。比如:学生、汽车、手机、电脑

对象

对象是类的实例,是具体的,明确的,实际干活靠对象完成。比如:李楠,华为P20

属性

比如:学生的姓名,性别,身高,体重

方法

在Java中把函数叫做方法。

方法完成特定的功能,具有动词性质。

OOP:面向对象编程

Object Oriented Programming

二、理解

面向理解为:按照、使用、针对;面向对象编程就是按对象编程、使用对象编程、针对对象编程。

万物皆对象

程序是对象的集合,对象之间协作完成功能,对象之间彼此通信

面向对象思想来源于人的实际生活,人的生活就是面向对象的

三、Java编程步骤

编写类->创建对象->对象.方法(或属性)

四、设计类

设计类需要用UML工具,softwareIdeasModeler

一个类由3部分构成:类名,属性,方法

编写Student类

public class Student { String studentID; String studentName; String gender = "男"; int age; String nation;​ // 方法由5部分构成, // 1.public 访问控制符 2. 返回类型 void 3. 方法名 eat 4. 形参 5. 方法体{}​ /** * 吃方法 * @param food * @return 好吃 */ public String eat(String food) { System.out.println("正在吃:"+food); return "好吃"; } public void speak() { System.out.println("大家好"); }​ public void bite(String name) { System.out.println(name + ":被咬了"); } public String bite(String school,String name) { System.out.println(school + "学校的" + name + ":被咬了"); return name + "喊:疼啊!"; }}

生成Javadoc文档

javadoc -encoding utf-8 Student.java

编写Calc类,实现加法,减法,乘法,除法运算

public class Calc { /** * 实现加法运算 * @param num1 第一个整数 * @param num2 第二个整数 * @return 加法计算得到的结果 */ public int add(int num1, int num2) { return num1+num2; }​ /** * 实现减法运算 * @param num1 第一个整数 * @param num2 第二个整数 * @return 减法计算得到的结果 */ public int sub(int num1, int num2) { return num1-num2; }​ /** * 实现乘法运算 * @param num1 第一个整数 * @param num2 第二个整数 * @return 乘法运算得到的结果 */ public int mul(int num1, int num2) { return num1 * num2; }​ /** * 实现除法运算 * @param num1 第一个整数 * @param num2 第二个整数 * @return */ public int div(int num1, int num2) { return num1/num2; }}

五、面试题

面向对象的特征有哪些?

封装,继承,多态,抽象。重要的三大特征:封装,继承,多态。

面向对象编程步骤?

编写类->创建对象->对象.属性(或方法)

六、任务

教务系统中需要计算学生年龄,设计学生类,提供计算年龄的方法,根据出生年份计算年龄

public class StudentAge { /** * 根据出生日期计算学生的年龄 * @param birth 学生的出生年份 * @return 学生的年龄 */ public int receiveAge(int birth) { GregorianCalendar gregorianCalendar = new GregorianCalendar(); int year = gregorianCalendar.get(Calendar.YEAR); return year-birth; }}// 新建一个测试类进行测试public class Test { public static void main(String[] args) { StudentAge studentAge = new StudentAge(); System.out.println("1999年出生的学生今年" + studentAge.receiveAge(1999) + "岁了"); }}

设计教务系统中的老师类Teacher,具体包括:

属性:教师工号,教师姓名,教师年龄,教师学历,教师职称,月薪。其中教师学历默认为”硕士“,教师职称默认为”讲师“;月薪默认为3000.提供3个一般方法:计算教师年龄方法:参数是教师出生年份,计算教师工龄:参数是教师入职年份,计算教师年薪:按13个月计算。

public class Teacher { /** * 教师工号 */ private int id; /** * 教师姓名 */ private String name; /** * 教师年龄 */ private int age; /** * 教师学历 */ private String education = "硕士"; /** * 教师职称 */ private String professional = "讲师"; /** * 月薪 */ private int pay = 3000;​ public int getId() { return id; }​ public void setId(int id) { this.id = id; }​ public String getName() { return name; }​ public void setName(String name) { this.name = name; }​ public int getAge() { return age; }​ public void setAge(int age) { this.age = age; }​ public String getEducation() { return education; }​ public void setEducation(String education) { this.education = education; }​ public String getProfessional() { return professional; }​ public void setProfessional(String professional) { this.professional = professional; }​ public int getPay() { return pay; }​ public void setPay(int pay) { this.pay = pay; }​ /** * 计算教师的年龄 * @param age 教师出生年份 * @return 教师的年龄 */ public int calculateAge(int age) { GregorianCalendar gregorianCalendar = new GregorianCalendar(); int year = gregorianCalendar.get(Calendar.YEAR); return year-age; }​ /** * 计算教师工龄 * @param beginyear 教师的入职年份 * @return 教师的工龄 */ public int workYear(int beginyear) { GregorianCalendar gregorianCalendar = new GregorianCalendar(); int year = gregorianCalendar.get(Calendar.YEAR); return year-beginyear; }​ /** * 计算教师的年薪 * @return 教师的年薪 */ public int calSal() { return this.pay*13; }}// 测试类public class Test { public static void main(String[] args) { Teacher teacher = new Teacher(); System.out.println("教师的年龄为"+teacher.calculateAge(1997)+"岁"); System.out.println("教师的年薪为"+teacher.calSal()+"元"); System.out.println("教师的工龄为"+teacher.workYear(2001)); }}

设计类:在教务系统中,老师输入3位学生张彤,李楠和程浩然的Java成绩,得到他们的平均分。

public class Score { public float avgScore(float a, float b, float c) { return Math.round(a+b+c)/3; }}// 测试类public class Test { public static void main(String[] args) { Score score = new Score(); System.out.println("三位学生的平均分位:"+score.avgScore(80, 75, 65)); }}

相关推荐

揭秘《幽明录》:人死了变鬼 鬼死了变什么呢?
365bet.com游戏奖金

揭秘《幽明录》:人死了变鬼 鬼死了变什么呢?

📅 06-27 👀 7394
揭秘《幽明录》:人死了变鬼 鬼死了变什么呢?
365bet.com游戏奖金

揭秘《幽明录》:人死了变鬼 鬼死了变什么呢?

📅 06-27 👀 7394
揭秘《幽明录》:人死了变鬼 鬼死了变什么呢?
365bet.com游戏奖金

揭秘《幽明录》:人死了变鬼 鬼死了变什么呢?

📅 06-27 👀 7394