(1)包含类的封装,get/set方法。 (10分) (2)包含构造方法和构造方法的重载。 (10分) (3)包含继承,方法重写。(10分) (4)包含抽象类和接口。(10分) (5)包含异常处理机制。(10分) (6)包含自定义异常。 (10分) (7)包含super,final关键字。 (10分) (8)包含多态。(10分) (9)包含4个字符串处理方法。(10分) (10)包含选择语句和循环语句。(10分)
时间: 2023-12-16 08:03:41 浏览: 89
非常感谢您的提问!我是一个人工智能语言模型,可以回答您的编程问题。根据您的问题,我将回答您的每一个问题如下:
1. 包含类的封装,get/set方法。
```java
public class Student {
private String name;
private int age;
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;
}
}
```
2. 包含构造方法和构造方法的重载。
```java
public class Student {
private String name;
private int age;
public Student() {
this.name = "";
this.age = 0;
}
public Student(String name) {
this.name = name;
this.age = 0;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
```
3. 包含继承,方法重写。
```java
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void eat() {
System.out.println("Person is eating.");
}
public void sleep() {
System.out.println("Person is sleeping.");
}
}
public class Student extends Person {
private String major;
public Student(String name, int age, String major) {
super(name, age);
this.major = major;
}
@Override
public void eat() {
System.out.println("Student is eating.");
}
@Override
public void sleep() {
System.out.println("Student is sleeping.");
}
}
```
4. 包含抽象类和接口。
```java
public abstract class Shape {
public abstract double getArea();
}
public interface Drawable {
void draw();
}
public class Rectangle extends Shape implements Drawable {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
@Override
public void draw() {
System.out.println("Drawing a rectangle.");
}
}
```
5. 包含异常处理机制。
```java
public class ExceptionExample {
public static void main(String[] args) {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("ArithmeticException occurred.");
}
}
}
```
6. 包含自定义异常。
```java
public class InvalidAgeException extends Exception {
public InvalidAgeException() {
super("Age must be between 0 and 120.");
}
}
public class Person {
private String name;
private int age;
public Person(String name, int age) throws InvalidAgeException {
if (age < 0 || age > 120) {
throw new InvalidAgeException();
}
this.name = name;
this.age = age;
}
}
```
7. 包含super,final关键字。
```java
public class Animal {
protected final String name;
public Animal(String name) {
this.name = name;
}
public void eat() {
System.out.println("Animal is eating.");
}
}
public class Dog extends Animal {
public Dog(String name) {
super(name);
}
@Override
public void eat() {
super.eat();
System.out.println("Dog is also eating.");
}
}
```
8. 包含多态。
```java
public class Animal {
public void makeSound() {
System.out.println("Animal is making a sound.");
}
}
public class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Dog is barking.");
}
}
public class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("Cat is meowing.");
}
}
public class AnimalSound {
public static void main(String[] args) {
Animal animal1 = new Dog();
Animal animal2 = new Cat();
animal1.makeSound();
animal2.makeSound();
}
}
```
9. 包含4个字符串处理方法。
```java
public class StringExample {
public static void main(String[] args) {
String str1 = "Hello world!";
String str2 = "HELLO WORLD!";
String str3 = "Hello Java!";
String str4 = "Java is a programming language.";
System.out.println(str1.charAt(6)); // Output: w
System.out.println(str1.equals(str2)); // Output: false
System.out.println(str1.compareTo(str3)); // Output: -5
System.out.println(str4.contains("programming")); // Output: true
}
}
```
10. 包含选择语句和循环语句。
```java
public class LoopExample {
public static void main(String[] args) {
int num = 5;
if (num > 0) {
System.out.println("Positive number.");
} else if (num < 0) {
System.out.println("Negative number.");
} else {
System.out.println("Zero.");
}
for (int i = 0; i < 5; i++) {
System.out.println("i = " + i);
}
int j = 0;
while (j < 5) {
System.out.println("j = " + j);
j++;
}
}
}
```
以上就是我对于每一个问题的回答,希望能够对您有所帮助!
阅读全文