Java-oop

166 阅读4分钟

Java面向对象1

  1. 编写一个Java程序,创建一个名为“Person”的类,带有名称和年龄属性。创建“Person”类的两个实例,使用构造函数设置它们的属性,并打印它们的名称和年龄。

    // Person.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;
        }
    }
    
    // Main.java
    public class Main {
      public static void main(String[] args) {
        Person person1 = new Person("Ean Craig", 11);
        Person person2 = new Person("Evan Ross", 12);
        System.out.println(person1.getName() + " is " + person1.getAge() + " years old.");
        System.out.println(person2.getName() + " is " + person2.getAge() + " years old.\n");
      }
    }
    

    上面的类有两个私有属性:name和age,以及一个使用作为参数传递的值初始化这些属性的构造函数。它还有一个getter方法来访问属性。在上面的示例中,我们创建了“Person”类的两个实例,使用构造函数设置它们的属性,并使用getter方法打印它们的名称和年龄。我们还使用setter方法修改属性并打印更新的值。

2.编写一个Java程序来创建一个名为“Dog”的类,带有名称和品种属性。创建“Dog”类的两个实例,使用构造函数设置它们的属性,使用setter方法修改属性并打印更新的值。

// Dog.java
public class Dog {
  private String name;
  private String breed;

  public Dog(String name, String breed) {
    this.name = name;
    this.breed = breed;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getBreed() {
    return breed;
  }

  public void setBreed(String breed) {
    this.breed = breed;
  }
}

上面的类有两个私有属性:“名称”和“品种”,以及一个使用作为参数传递的值初始化这些属性的构造函数。它还具有getter和setter方法来访问和修改这些属性。

// Main.java
public class Main {
  public static void main(String[] args) {
    Dog dog1 = new Dog("Buddy", "Golden Retriever");
    Dog dog2 = new Dog("Charlie", "Bulldog");

    System.out.println(dog1.getName() + " is a " + dog1.getBreed() + ".");
    System.out.println(dog2.getName() + " is a " + dog2.getBreed() + ".");

    System.out.println("\nSet the new Breed of dog1 and new name of dog2:\n");
    dog1.setBreed("Labrador Retriever");
    dog2.setName("Daisy");

    System.out.println(dog1.getName() + " is now a " + dog1.getBreed() + ".");
    System.out.println(dog2.getName() + " is now a " + dog2.getBreed() + ".");
  }
}

在上面的示例代码中,我们创建了“Dog”类的两个实例,通过构造函数设置它们的属性,并使用getter方法打印它们的名称和品种。我们还使用setter方法修改属性并打印更新的值。

3.编写一个Java程序,创建一个名为“矩形”的类,具有宽度和高度属性。计算矩形的面积和周长。

//Rectangle.java
public class Rectangle {
  private double width;
  private double height;

  public Rectangle(double width, double height) {
    this.width = width;
    this.height = height;
  }
  public double getWidth() {
    return width;
  }
  public void setWidth(double width) {
    this.width = width;
  }
  public double getHeight() {
    return height;
  }
  public void setHeight(double height) {
    this.height = height;
  }
  public double getArea() {
    return width * height;
  }
  public double getPerimeter() {
    return 2 * (width + height);
  }
}

上面的类有两个私有属性:“宽度”和“高度”,一个用作为参数传递的值初始化这些属性的构造函数,以及访问和修改这些属性的getter和setter方法。它还有两个方法“getArea()”和“getPerimeter()”来计算矩形的面积和周长。

//Main.java
public class Main {
  public static void main(String[] args) {
    Rectangle rectangle = new Rectangle(7, 12);

    System.out.println("The area of the rectangle is " + rectangle.getArea());
    System.out.println("The perimeter of the rectangle is " + rectangle.getPerimeter());

    rectangle.setWidth(6);
    rectangle.setHeight(12);

    System.out.println("\nThe area of the rectangle is now " + rectangle.getArea());
    System.out.println("The perimeter of the rectangle is now " + rectangle.getPerimeter());
  }
}

在Main()函数中,我们创建了一个宽度为7、高度为12的“矩形”类的实例,并调用它的方法来计算面积和周长。然后我们使用setter方法修改宽度和高度,并打印更新后的矩形区域和周长。

4.编写一个Java程序来创建一个名为“Circle”的类,带有半径属性。您可以访问和修改此属性。计算圆的面积和周长。

//Circle.java
public class Circle {
  private double radius;

  public Circle(double radius) {
    this.radius = radius;
  }

  public double getRadius() {
    return radius;
  }

  public void setRadius(double radius) {
    this.radius = radius;
  }

  public double getArea() {
    return Math.PI * radius * radius;
  }

  public double getCircumference() {
    return 2 * Math.PI * radius;
  }
}

上面的“Circle”类有一个私有属性“半径”,一个用作为参数传递的值初始化这个属性的构造函数,以及访问和修改这个属性的getter和setter方法。它还使用方法计算圆的面积和周长。

// Main.java
public class Main {
  public static void main(String[] args) {
    int r = 5;
    Circle circle = new Circle(r);
    System.out.println("Radius of the circle is " + r);
    System.out.println("The area of the circle is " + circle.getArea());
    System.out.println("The circumference of the circle is " + circle.getCircumference());
    r = 8;
    circle.setRadius(r);
    System.out.println("\nRadius of the circle is " + r);
    System.out.println("The area of the circle is now " + circle.getArea());
    System.out.println("The circumference of the circle is now " + circle.getCircumference());
  }
}

在上面的main()函数中,我们创建了一个半径为5的“Circle”类的实例,并调用它的方法来计算面积和周长。然后我们使用setter方法修改半径并打印更新后的面积和周长。