public class Addition { // 无参构造方法 public Addition(){} // add方法,计算int类型相加的结果 public int add(int a, int b){ return a + b; } // add方法,计算long类型相加的结果 public long add(long a, long b){ return a + b; } // add方法,计算double类型相加的结果 public double add(double a, double b){ return a + b; } // add方法,计算float类型相加的结果 public float add(float a, float b){ return a + b; } // main方法 public static void main(String[] args){ Addition addition = new Addition(); int result1 = addition.add(1, 2); long result2 = addition.add(2L, 3L); double result3 = addition.add(3.0, 4.0); float result4 = addition.add(4.0f, 5.0f); System.out.println("1 + 2 = " + result1); System.out.println("2L + 3L = " + result2); System.out.println("3.0 + 4.0 = " + result3); System.out.println("4.0f + 5.0f = " + result4); } }
时间: 2024-02-14 14:35:58 浏览: 424
这段代码定义了一个名为 Addition 的类,其中包含了四个重载的 add 方法,分别用于计算 int、long、double、float 类型的加法运算。在 main 方法中,我们分别创建了 Addition 类的对象 addition,并调用了四个不同类型的 add 方法,将结果保存在不同类型的变量中,并使用 System.out.println 方法输出了这些结果。最终结果应该是:1 + 2 = 3,2L + 3L = 5,3.0 + 4.0 = 7.0,4.0f + 5.0f = 9.0。
相关问题
编程题 1.医生有姓名(name)、编号(idNumber)、地址(address)等基本信息,可分为专业医生和非专业医生两类,所具有的信息与所列的基本信息一样,但是专业医生除了具有基本信息外,还有特定信息——专业(speciality)。根据它们之间的关系,创建一个Doctor类,包含医生的基本信息;利用继承来创建一个非专业医生类NonSpecialist和专业医生类Specialist,并利用这些类的对象来显示它们各自的信息。 2.编写一个程序,重写父类Addition中名为add()的抽象方法。在NumberAddition类中,add()用于将两个数字相加;在TextConcatenation类中,add()用于连接两个Strings(字符串)。声明属性,并在父类Addition的构造方法中初始化属性。
1. Doctor类的实现:
```java
public class Doctor {
private String name;
private int idNumber;
private String address;
public Doctor(String name, int idNumber, String address) {
this.name = name;
this.idNumber = idNumber;
this.address = address;
}
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("ID Number: " + idNumber);
System.out.println("Address: " + address);
}
}
class NonSpecialist extends Doctor {
public NonSpecialist(String name, int idNumber, String address) {
super(name, idNumber, address);
}
}
class Specialist extends Doctor {
private String speciality;
public Specialist(String name, int idNumber, String address, String speciality) {
super(name, idNumber, address);
this.speciality = speciality;
}
@Override
public void displayInfo() {
super.displayInfo();
System.out.println("Speciality: " + speciality);
}
}
```
2. Addition类的实现:
```java
public abstract class Addition {
protected String attribute;
public Addition(String attribute) {
this.attribute = attribute;
}
public abstract void add();
}
class NumberAddition extends Addition {
private int num1;
private int num2;
public NumberAddition(String attribute, int num1, int num2) {
super(attribute);
this.num1 = num1;
this.num2 = num2;
}
@Override
public void add() {
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
}
}
class TextConcatenation extends Addition {
private String str1;
private String str2;
public TextConcatenation(String attribute, String str1, String str2) {
super(attribute);
this.str1 = str1;
this.str2 = str2;
}
@Override
public void add() {
System.out.println(str1 + " " + attribute + " " + str2);
}
}
```
使用示例:
```java
public class Main {
public static void main(String[] args) {
// 创建医生对象并显示信息
Doctor doctor1 = new NonSpecialist("John Doe", 12345, "123 Main St.");
doctor1.displayInfo();
Doctor doctor2 = new Specialist("Jane Smith", 67890, "456 Broadway", "Cardiology");
doctor2.displayInfo();
// 进行加法操作
Addition addition1 = new NumberAddition("plus", 2, 3);
addition1.add();
Addition addition2 = new TextConcatenation("and", "Hello", "world!");
addition2.add();
}
}
```
输出结果:
```
Name: John Doe
ID Number: 12345
Address: 123 Main St.
Name: Jane Smith
ID Number: 67890
Address: 456 Broadway
Speciality: Cardiology
2 + 3 = 5
Hello and world!
```
编写Addition类,该类别
编写一个名为`Addition`的类,这个类通常用于表示数值的加法操作,并可能提供一些基本的方法来进行加法计算。在面向对象编程中,这样的类可以封装数据和行为,以便更好地管理数值相加的过程。以下是`Addition`类的一个简单示例:
```java
public class Addition {
// 数据成员,如果需要保存加数,可以添加私有变量
private int num1;
private int num2;
// 构造函数,初始化加数
public Addition(int a, int b) {
this.num1 = a;
this.num2 = b;
}
// 提供公共方法来执行加法操作
public int add() {
return num1 + num2;
}
// 可选的:提供方法设置加数
public void setNumbers(int first, int second) {
num1 = first;
num2 = second;
}
// 可选的:提供方法获取加数
public int[] getNumbers() {
return new int[]{num1, num2};
}
// 如果需要支持链式调用,可以考虑提供一个加法方法链
public Addition add(int additional) {
setNumbers(getNumbers()[0], getNumbers()[1] + additional);
return this; // 返回自身实例以便链式调用
}
}
阅读全文