什么是oop思想_什么是OOPS

什么是oop思想

OOPS is a programming approach which provides solution to real life problems with the help of algorithms based on real world. It uses real world approach to solve a problem. So object oriented technique offers better and easy way to write program then procedural programming languages such as C, ALGOL, PASCAL etc. Click here to watch video on OOPS concept in Java

OOPS是一种编程方法,可借助基于现实世界的算法为现实生活中的问题提供解决方案。 它使用现实世界的方法来解决问题。 因此,面向对象技术提供了比程序编程语言(例如C,ALGOL,PASCAL等)更好,更轻松的方法来编写程序。 单击此处,观看Java上OOPS概念的视频。

Java is an object oriented language which supports object oriented concepts like: class and object. In OOPS data is treated important and encapsulated within the class, object then use to access that data during runtime.

Java是一种面向对象的语言,它支持诸如类和对象之类的面向对象的概念。 在OOPS中,数据被视为重要数据并封装在class ,对象中,然后用于在运行时访问该数据。

OOPS provides advantages over the other programming paradigm and include following features.

OOPS提供了优于其他编程范例的优势,并包括以下功能。

OOPS的主要功能 (Main Features of OOPS)

  • Inheritence

    传承

  • Polymorphism

    多态性

  • Encapsulation

    封装形式

  • Abstraction

    抽象化

As an object oriented language Java supports all the features given above. We will discuss all these features in detail later.

作为一种面向对象的语言,Java支持上述所有功能。 稍后我们将详细讨论所有这些功能。

Java类 (Java Class)

In Java everything is encapsulated under classes. Class is the core of Java language. It can be defined as a template that describe the behaviors and states of a particular entity.

在Java中,所有内容都封装在class下 。 类是Java语言的核心。 可以将其定义为描述特定实体的行为和状态的模板

A class defines new data type. Once defined this new type can be used to create object of that type.

一个类定义新的数据类型。 定义后,此新类型可用于创建该类型的对象。

Object is an instance of class. You may also call it as physical existence of a logical template class.

对象是class的实例 。 您也可以将其称为逻辑模板类的物理存在。

In Java, to declare a class class keyword is used. A class contain both data and methods that operate on that data. The data or variables defined within a class are called instance variables and the code that operates on this data is known as methods.

在Java中,使用class class关键字声明一个类。 一个类包含数据和对该数据进行操作的方法 。 在类中定义的数据或变量称为实例变量 ,对这些数据进行操作的代码称为方法

Thus, the instance variables and methods are known as class members.

因此,实例变量和方法称为类成员

Java类规则 (Rules for Java Class)

  • A class can have only public or default(no modifier) access specifier.

    一个类只能具有public或default(无修饰符)访问说明符。

  • It can be either abstract, final or concrete (normal class).

    它可以是抽象的,最终的或具体的(普通类)。

  • It must have the class keyword, and class must be followed by a legal identifier.

    它必须具有class关键字 ,并且class后面必须带有合法标识符。

  • It may optionally extend only one parent class. By default, it extends Object class.

    它可以选择仅扩展一个父类。 默认情况下,它扩展了Object类。

  • The variables and methods are declared within a set of curly braces.

    变量和方法在花括号内声明。

A Java class can contains fields, methods, constructors, and blocks. Lets see a general structure of a class.

Java类可以包含字段,方法,构造函数和块。 让我们看一看一个类的一般结构。

Java类语法 (Java class Syntax)
class  class_name {
     field;
     method;
}

一个简单的类示例 (A simple class example)

Suppose, Student is a class and student's name, roll number, age are its fields and info() is a method. Then class will look like below.

假设, Student是一个班级,并且学生的姓名,卷名,年龄是其字段,info()是一个方法 。 然后,类将如下所示。

class Student.
{
 String name;
 int rollno;
 int age;
void info(){
 // some code
}
}

This is how a class look structurally. It is a blueprint for an object. We can call its fields and methods by using the object.

这是类在结构上的外观。 它是对象的蓝图。 我们可以通过使用对象来调用其字段和方法。

The fields declared inside the class are known as instance variables. It gets memory when an object is created at runtime.

在类内部声明的字段称为实例变量 。 在运行时创建对象时,它将获取内存。

Methods in the class are similar to the functions that are used to perform operations and represent behavior of an object.

该类中的方法类似于用于执行操作并表示对象行为的函数。

After learning about the class, now lets understand what is an object.

了解了该类之后,现在让我们了解什么是对象。

Java对象 (Java Object)

Object is an instance of a class while class is a blueprint of an object. An object represents the class and consists of properties and behavior.

对象是类的实例,而类是对象的蓝图。 对象代表类,由属性行为组成

Properties refer to the fields declared with in class and behavior represents to the methods available in the class.

属性是指在类中用声明的字段,而行为是指类中可用的方法。

In real world, we can understand object as a cell phone that has its properties like: name, cost, color etc and behavior like calling, chatting etc.

在现实世界中,我们可以将对象理解为一部手机,其属性包括:名称,成本,颜色等,以及诸如通话,聊天等行为。

So we can say that object is a real world entity. Some real world objects are: ball, fan, car etc.

因此,我们可以说该对象是真实世界的实体。 现实世界中的一些物体是:球,风扇,汽车等。

There is a syntax to create an object in the Java.

有一种在Java中创建对象的语法。

Java对象语法 (Java Object Syntax)
className variable_name = new className();

Here, className is the name of class that can be anything like: Student that we declared in the above example.

在这里, className是类的名称,可以类似于:在上面的示例中声明的Student。

variable_name is name of reference variable that is used to hold the reference of created object.

variable_name是引用变量的名称,用于保存所创建对象的引用

The new is a keyword which is used to allocate memory for the object.

new是一个关键字,用于为对象分配内存。

Lets see an example to create an object of class Student that we created in the above class section.

让我们看一个示例,该示例创建在上面的课程部分中创建的Student类的对象。

Although there are many other ways by which we can create object of the class. we have covered this section in details in a separate topics.

尽管还有许多其他方法可以创建类的对象。 我们在单独的主题中详细介绍了本节。

示例:对象创建 (Example: Object creation)
Student std = new Student();

Here, std is an object that represents the class Student during runtime.

在此, std是一个在运行时表示Student类的对象。

The new keyword creates an actual physical copy of the object and assign it to the std variable. It will have physical existence and get memory in heap area. The new operator dynamically allocates memory for an object.

new关键字创建对象的实际物理副本,并将其分配给std变量。 它将具有物理存在并在堆区域中获得内存。 new运算符为对象动态分配内存

creation of object in java

In this image, we can get idea how the an object refer to the memory area.

在此图中,我们可以了解一个对象如何引用存储区域。

Now lets understand object and class combinally by using a real example.

现在,通过一个真实的例子来组合地理解对象和类。

示例:创建一个类及其对象 (Example: Creating a Class and its object)
public class Student{      
 
	 String name;
	 int rollno;
	 int age;
	 
	void info(){
	  System.out.println("Name: "+name);
	  System.out.println("Roll Number: "+rollno);
	  System.out.println("Age: "+age);
	}  
	
	
	public static void main(String[] args) {
		Student student = new Student();
		
		// Accessing and property value
		student.name = "Ramesh";
		student.rollno = 253;
		student.age = 25;
		
		// Calling method
		student.info();
		
	}
}

Name: Ramesh Roll Number: 253 Age: 25

名称:拉梅什卷编号:253年龄:25

In this example, we created a class Student and an object. Here you may be surprised of seeing main() method but don’t worry it is just an entry point of the program by which JVM starts execution.

在此示例中,我们创建了一个Student类和一个对象。 在这里,您可能会惊讶地看到main()方法,但不必担心,它只是JVM开始执行的程序的入口点。

Here, we used main method to create object of Student class and access its fields and methods.

在这里,我们使用main方法来创建Student类的对象并访问其字段和方法。

示例:类字段 (Example: Class fields)
public class Student{      
 
	 String name;
	 int rollno;
	 int age;
	 
	void info(){
	  System.out.println("Name: "+name);
	  System.out.println("Roll Number: "+rollno);
	  System.out.println("Age: "+age);
	}  
	
	
	public static void main(String[] args) {
		Student student = new Student();
		
		// Calling method
		student.info();
		
	}
}

Name: null Roll Number: 0 Age: 0

名称:空卷编号:0年龄:0

In case, if we don’t initialized values of class fields then they are initialized with their default values.

万一,如果我们不初始化类字段的值,那么它们将使用其默认值进行初始化。

实例变量的默认值 (Default values of instance variables )

int, byte, short, long -> 0

int,字节,短,长-> 0

float, double → 0.0

浮点,双重→0.0

string or any reference = null

字符串或任何引用= null

boolean → false

布尔→假

These values are initialized by the default constructor of JVM during object creation at runtime.

这些值由JVM的默认构造函数在运行时创建对象期间初始化。

翻译自: https://www.studytonight.com/java/object-and-classes.php

什么是oop思想

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值