我是面向对象编程的新手,但我不明白主对象的目的是什么。
是的,我读到它是程序的“入口点”,但是我不明白的是程序的主要内容是什么?它的职责是什么?
可能会发生将主体编写的内容封装到另一个对象中的情况,但是您应该使用这种方法多少呢?
这是我用Java编写的第一个主语言,它很简单,但是可能会让您更好地理解我的疑问。我有一个抽象类Animal,它由“ Cat”和“ Dog”扩展。我使用main来创建一些对象,并使用它作为与用户的“接口”,确实如您所见,我使用了一些条件指令来“询问用户”他想做什么。
我的问题来自于这样一个事实,即接口可以封装在另一个对象中,而不是把责任交给主要对象。
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("What type of animal do you want to create? \n dog cat");
    String type = input.nextLine();
    if ( Objects.equals(type, "dog")){
        System.out.println("Enter the animal's age: ");
        int age = input.nextInt(); // Scans the next token of the input as an int.
        System.out.println("Enter the animal's name: ");
        String name = input.next();
        Dog first = new Dog(name, age);
    }
    else if ( Objects.equals(type, "cat")) {
        System.out.println("Enter the animal's age: ");
        int age = input.nextInt(); // Scans the next token of the input as an int.
        System.out.println("Enter the animal's name: ");
        String name = input.next();
        Cat first = new Cat(name, age);
    }
    else{
        System.out.println("Error: the specified type does not exist.");
    }
    System.out.println("The number of animals is:" + numberOfAnimals);
}
main功能不是OOP的概念。