此Java代码中的对象在哪里(Where is the object in this Java code)

编程入门 行业动态 更新时间:2024-10-27 12:40:13
此Java代码中的对象在哪里(Where is the object in this Java code) java

我刚开始使用Java学习OOP。 我写的第一个代码是:

class day1 { public static void main(String args[]) { System.out.println("Java drives the web"); } }

当我编译这个源文件时,我得到一个.class文件。 当我在命令行解释它时,它给了我想要的结果。

我的问题是:我没有创建任何对象。 我刚刚写了一个名为day1的类。 我怎么能解释它? OOP是关于从类中提取的对象。 但我没有从课堂上抽出任何东西1。

我在OOP中感到困惑。

I just started learning OOP using Java. First code I wrote is :

class day1 { public static void main(String args[]) { System.out.println("Java drives the web"); } }

When I compile this source file, I get a .class file. When I interpret it at command line, it gives me the desired result.

My question is: I have not created any object. I have just wrote a class named day1. How am I able to interpret it? OOP is about object drawn from the classes. But I have not drawn anything from the class day1.

I am confused in OOP.

最满意答案

面向对象编程是一种范例,提供了诸如继承封装多态等许多概念。令人惊讶的是, 这三个主要的OOP概念也存在于您的小型简单Hello World程序中 。 我想证明这一点,请在下面找到澄清说明

1.在程序中使用继承概念

java.lang包中的Object类位于类层次结构树的顶部。 每个类都是Object类的直接或间接的后代。 您使用或编写的每个类都继承Object的实例方法。

因此,虽然您没有扩展任何类,但仍然是您的Day1类隐式扩展Object类并继承Object类的所有方法。 所以你已经在这里使用了继承概念。

2.在程序中使用Encapsulation概念

Java或面向对象编程语言中的封装是一种强制保护变量,来自类外的函数的概念,以便更好地管理该段代码,并且由于受保护代码的变化而对程序的其他部分影响最小或没有影响。

封装可以通过Java中的访问说明符来实现,并且您已经在程序中应用了封装概念您使用访问说明符Public声明了您的main方法 - 以便JVM可以调用main方法。 您将此公开以允许来自应用程序外部的呼叫。

3.在您的程序中使用多态性概念

面向对象编程环境中的多态性是创建具有多个表单的变量,函数或对象的能力

System.out.println("Java drives the web");

这里使用了PrintStream类的println()方法。 如果您转到PrintStream类的源代码,您会发现此类中有十个方法,它们具有相同的名称println但具有不同的参数。 简而言之,PrintStream类包含不同的重载方法所以你也在这里使用了编译时多态(静态绑定或方法重载)。

如果你只是谈论对象,那么你可能从@MadConan得到了答案

来自@ MadConan的回答

代码中有许多对象

class day1:这是你的班级。 类是一个对象。 args []:这是array类型的对象。 系统: System对象随处可用。 它是核心API的一部分。 out:这是PrintWriter对象实例,它是System对象的成员。 “Java驱动网络”:这是一个String对象。

仅仅因为你从不使用new关键字并不意味着你没有使用任何对象。

Object Oriented Programming is a paradigm that provides many concepts such as inheritance, encapsulation, polymorphism etc. surprisingly these three major OOPs concept are present in your small simple Hello World program also. I am trying to prove that, please find below the clarifications

1. Use of Inheritance concept in your program

The Object class, in the java.lang package, sits at the top of the class hierarchy tree. Every class is a descendant, direct or indirect, of the Object class. Every class you use or write inherits the instance methods of Object.

So although you did not extend any class but still your Day1 class implicitly extending Object class and inherits all the methods of Object class. So you already used inheritance concept here.

2. Use of Encapsulation concept in your program

Encapsulation in Java or object oriented programming language is a concept which enforce protecting variables, functions from outside of class, in order to better manage that piece of code and having least impact or no impact on other parts of program due to change in protected code.

Encapsulation can be achieved via access specifiers in Java and you already applied encapsulation concept in your program. You declared your main method with access specifier Public – so that main method can be called by JVM. You made this public to permit call from outside of the application.

3. Use of Polymorphism concept in your program

Polymorphism in the context of object-oriented programming, is the ability to create a variable, a function, or an object that has more than one form

System.out.println("Java drives the web");

Here you used println() method of PrintStream class. If you go to the source code of the PrintStream class, you will find ten methods are present in this class with same name println but with different parameters.In short PrintStream class contains different overloaded method. So you also used Compile time polymorphism (static binding or method overloading) here.

If you are only talking about object, then probably you got your answer from @MadConan

Answer from @MadConan

There are many objects in your code

class day1: This is your Class. Class is an object. args[]: This is an object of type array. System: The System object is available everywhere. It's part of the core API. out: This is the PrintWriter object instance which is a member of the System object. "Java drives the web": That is a String object.

Just because you never use the new keyword doesn't mean you aren't using any objects.

更多推荐

本文发布于:2023-08-04 15:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417743.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   代码   Java   object   code

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!