面向对象的语言和非面向对象的语言有什么区别? [英] What is the difference between object-oriented languages and non object-oriented languages?

查看:561
本文介绍了面向对象的语言和非面向对象的语言有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在听说C如何是一种非面向对象的语言,而Java如何是一种面向对象的语言.我想知道有什么区别吗?

I have been hearing about how C is a non-object-oriented language and how java is an object-oriented language. I was wondering what the difference was?

推荐答案

哇,这个家伙抛出了很多重要的OOP术语.作为一个开始于过程编程并且现在主要从事OOP的人,从概念上讲,这就是我如何看待差异的原因(没有什么大的术语):

Wow, a lot of big OOP terms being thrown around to this guy. Being one who started in procedural programming and is now mostly doing OOP, this is conceptually how I think of the difference (without all the big terms):

在C语言中,您拥有可以保持状态的结构.它们看起来像对象,例如,您可以拥有一个名为Car的结构,并创建Cars的实例,并设置其make,model和color字段.但是,您不能告诉Car结构实例执行任何操作.相反,如果您要洗车,则必须将汽车实例传递给某些外部函数,如下所示:

In C, you have things called structs that can hold state. They kind of seem like objects, for example you could have a struct called Car and create instances of Cars and set its make, model, and color fields. However, you cannot tell the Car struct instances to do anything. Instead, if you want to wash your car, you have to pass the car instance to some external function like this:

WashMyCar(myCar);

OOP语言使用与称为类的结构不同的概念,对象是这些类的实例.现在就不要再考虑那些大的单词继承和多态了(一旦获得类,这些将是更高级的主题).试想一下汽车的例子.例如,在Java中,您可以这样定义一个名为Car的类:

OOP languages use a different concept from structs called Classes, and objects are instances of those classes. Forget about those big words inheritance and polymorphism for now (those are more advanced topics for once you kind of get Classes). Just think of the example of a car. In Java, for example, you could define a class called Car as such:

public class Car {
  String make;
  String model;
  String color;
}

然后,您像这样制作汽车的实例:

Then, you make an instance of a car like so:

Car myCar = new Car();
myCar.make = "Honda";
myCar.model = "Accord";
myCar.color = "Black";

这确实类似于结构.现在,使OOP与众不同的是,您可以扩展类"定义以定义类方法-与过程中的函数相似,不同之处在于它们始终对对象进行操作.因此,让我们添加洗涤方法:

This is real similar to a struct. Now, what makes OOP different is that you can expand the Class definition to define class methods - which are similar to functions in procedural except that they always operate on an object. So, let's add the wash method:

public class Car {
  String make;
  String model;
  String color;
  String condition;
  void washMe() {
    this.condition = "clean";
  }
  void goOffroad() {
    this.condition = "dirty";
  }
}

现在您可以执行以下操作:

Now you can do this:

Car myCar = new Car();
myCar.make = "Honda";
myCar.model = "Accord";
myCar.color = "Black";

myCar.goOffroad();
System.out.println(myCar.condition); // dirty
myCar.washMe();
System.out.println(myCar.condition); // clean

希望这个例子有帮助.当然,除了这个简单的示例外,OOP(和过程)还有更多的东西.但是核心区别在于拥有拥有"自己方法的对象类.

Hopefully this example helps. There is, of course, much more to OOP (and procedural) than this simple example. But the core difference is in having classes of objects that "own" their own methods.

这篇关于面向对象的语言和非面向对象的语言有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆