I'm having a little trouble getting my head around the conceptual difference between an object and a class. I don't really understand the distinction between the two in any programming language, but currently I'm working with Perl, and Moose, so I'd prefer an explanation using those things.
我在解决一个对象和一个类之间的概念差异时遇到了一些麻烦。我真的不理解任何编程语言中两者之间的区别,但目前我正在使用Perl和Moose,所以我更喜欢使用这些东西进行解释。
Cheers
8 个解决方案
#1
There are lots of "a class is a blueprint, an object is something built from that blueprint", but since you've asked for a specific example using Moose and Perl, I thought I'd provide one.
有很多“一个类是一个蓝图,一个对象是从该蓝图构建的东西”,但是既然你已经要求使用Moose和Perl的一个具体例子,我想我会提供一个。
In this following example, we're going have a class named 'Hacker'. The class (like a blueprint) describes what hackers are (their attributes) and what they can do (their methods):
在下面的示例中,我们将有一个名为“Hacker”的类。该类(如蓝图)描述了黑客是什么(他们的属性)以及他们可以做什么(他们的方法):
package Hacker; # Perl 5 spells 'class' as 'package'
use Moose; # Also enables strict and warnings;
# Attributes in Moose are declared with 'has'. So a hacker
# 'has' a given_name, a surname, a login name (which they can't change)
# and a list of languages they know.
has 'given_name' => (is => 'rw', isa => 'Str');
has 'surname' => (is => 'rw', isa => 'Str');
has 'login' => (is => 'ro', isa => 'Str');
has 'languages' => (is => 'rw', isa => 'ArrayRef[Str]');
# Methods are what a hacker can *do*, and are declared in basic Moose
# with subroutine declarations.
# As a simple method, hackers can return their full name when asked.
sub full_name {
my ($self) = @_; # $self is my specific hacker.
# Attributes in Moose are automatically given 'accessor' methods, so
# it's easy to query what they are for a specific ($self) hacker.
return join(" ", $self->given_name, $self->surname);
}
# Hackers can also say hello.
sub say_hello {
my ($self) = @_;
print "Hello, my name is ", $self->full_name, "\n";
return;
}
# Hackers can say which languages they like best.
sub praise_languages {
my ($self) = @_;
my $languages = $self->languages;
print "I enjoy programming in: @$languages\n";
return;
}
1; # Perl likes files to end in a true value for historical reasons.
Now that we've got our Hacker class, we can start making Hacker objects:
现在我们已经获得了Hacker类,我们可以开始制作Hacker对象了:
#!/usr/bin/perl
use strict;
use warnings;
use autodie;
use Hacker; # Assuming the above is in Hacker.pm
# $pjf is a Hacker object
my $pjf = Hacker->new(
given_name => "Paul",
surname => "Fenwick",
login => "pjf",
languages => [ qw( Perl C JavaScript) ],
);
# So is $jarich
my $jarich = Hacker->new(
given_name => "Jacinta",
surname => "Richardson",
login => "jarich",
languages => [ qw( Perl C Haskell ) ],
);
# $pjf can introduce themselves.
$pjf->say_hello;
$pjf->praise_languages;
print "\n----\n\n";
# So can $jarich
$jarich->say_hello;
$jarich->praise_languages;
This results in the following output:
这导致以下输出:
Hello, my name is Paul Fenwick
I enjoy programming in: Perl C JavaScript
----
Hello, my name is Jacinta Richardson
I enjoy programming in: Perl C Haskell
If I want I can have as many Hacker objects as I like, but there's still only one Hacker class that describes how all of these work.
如果我想要我可以拥有尽可能多的黑客对象,但仍然只有一个黑客类可以描述所有这些对象的工作原理。
All the best,
祝一切顺利,
Paul
#2
A class is a type (like "SUV"). An object is an instance of a class ("David's SUV").
类是一种类型(如“SUV”)。对象是类的实例(“David的SUV”)。
#3
Perl-wise:
- A class is a
package
--a specification. A set of behaviors and data mainly to aid those behaviors. - An object is typically a "hashref", that is a collection of specific data allowed by the behavior specification in the package (and inherited behaviors).
一个类是一个包 - 一个规范。一组行为和数据主要用于帮助这些行为。
对象通常是“hashref”,它是包中行为规范允许的特定数据的集合(以及继承的行为)。
Now, a hashref might hold a code reference. In most cases, that's behavior. But the only way the object could use that specific behavior is for that to be specified by some class behavior inherited (or mixed in) that expects that there might be a coderef sitting at that location and invoke it.
现在,hashref可能包含代码引用。在大多数情况下,这是行为。但是对象可以使用该特定行为的唯一方法是由一些继承(或混合)的类行为指定,该行为期望可能存在位于该位置并调用它的coderef。
#4
Another way to think of it is a class is a blueprint for how an object will be built.
另一种思考方式是类是如何构建对象的蓝图。
#5
Objects are single instances of a Class.
对象是类的单个实例。
#6
You are an object of class Human
你是人类的对象
(Classes in Perl are modules with some special qualities, you should better first understand only the general case).
(Perl中的类是具有一些特殊性质的模块,您最好先了解一般情况)。
#7
In perl class is nothing but it is a package name. It has a common code for the objects.
在perl类中,它只是一个包名。它有一个对象的公共代码。
object is a instance that has access the class's properties and methods.
object是一个可以访问类的属性和方法的实例。
package vehicle;
sub vehicle_detail
{
($number,$model,$num_of_wheel)=@_;
print "My car Details:\n@_";
}
The above class vehicle can be used by any vehicle such as bike,car,van..etc. The object is created by the operator bless.
上述级别的车辆可以被任何车辆使用,例如自行车,汽车,货车等。该对象由操作员保佑创建。
$bike_name='honda';
$ref_bike=\$bike_name;
bless $ref_bike,'vehicle';
Now the bless creates the object honda for the class vehicle.
现在,祝福为班级车辆创造了本田对象。
#8
I don't see people using the terms the same way in other languages. That may be one reason for the question. I think maybe PHP users say "class" when they should say "object", a lot of the time?
我不认为人们在其他语言中使用相同的术语。这可能是问题的一个原因。我想也许PHP用户在他们应该说“对象”时会说“课堂”,很多时候?
Anyway, what about this example -- imagine you had to create two different database connections for two different databases:
无论如何,这个例子怎么样 - 想象你必须为两个不同的数据库创建两个不同的数据库连接:
my $oracle_database_handle = DBI->connect( <oracle connection details here> );
my $mysql_database_handle = DBI->connect( <mysql connection details here> );
you would have created two objects for doing two different things, but they're both the same kind of thing -- DBI database connections.
你会创建两个对象来做两件事,但它们都是同一类东西--DBI数据库连接。
#1
There are lots of "a class is a blueprint, an object is something built from that blueprint", but since you've asked for a specific example using Moose and Perl, I thought I'd provide one.
有很多“一个类是一个蓝图,一个对象是从该蓝图构建的东西”,但是既然你已经要求使用Moose和Perl的一个具体例子,我想我会提供一个。
In this following example, we're going have a class named 'Hacker'. The class (like a blueprint) describes what hackers are (their attributes) and what they can do (their methods):
在下面的示例中,我们将有一个名为“Hacker”的类。该类(如蓝图)描述了黑客是什么(他们的属性)以及他们可以做什么(他们的方法):
package Hacker; # Perl 5 spells 'class' as 'package'
use Moose; # Also enables strict and warnings;
# Attributes in Moose are declared with 'has'. So a hacker
# 'has' a given_name, a surname, a login name (which they can't change)
# and a list of languages they know.
has 'given_name' => (is => 'rw', isa => 'Str');
has 'surname' => (is => 'rw', isa => 'Str');
has 'login' => (is => 'ro', isa => 'Str');
has 'languages' => (is => 'rw', isa => 'ArrayRef[Str]');
# Methods are what a hacker can *do*, and are declared in basic Moose
# with subroutine declarations.
# As a simple method, hackers can return their full name when asked.
sub full_name {
my ($self) = @_; # $self is my specific hacker.
# Attributes in Moose are automatically given 'accessor' methods, so
# it's easy to query what they are for a specific ($self) hacker.
return join(" ", $self->given_name, $self->surname);
}
# Hackers can also say hello.
sub say_hello {
my ($self) = @_;
print "Hello, my name is ", $self->full_name, "\n";
return;
}
# Hackers can say which languages they like best.
sub praise_languages {
my ($self) = @_;
my $languages = $self->languages;
print "I enjoy programming in: @$languages\n";
return;
}
1; # Perl likes files to end in a true value for historical reasons.
Now that we've got our Hacker class, we can start making Hacker objects:
现在我们已经获得了Hacker类,我们可以开始制作Hacker对象了:
#!/usr/bin/perl
use strict;
use warnings;
use autodie;
use Hacker; # Assuming the above is in Hacker.pm
# $pjf is a Hacker object
my $pjf = Hacker->new(
given_name => "Paul",
surname => "Fenwick",
login => "pjf",
languages => [ qw( Perl C JavaScript) ],
);
# So is $jarich
my $jarich = Hacker->new(
given_name => "Jacinta",
surname => "Richardson",
login => "jarich",
languages => [ qw( Perl C Haskell ) ],
);
# $pjf can introduce themselves.
$pjf->say_hello;
$pjf->praise_languages;
print "\n----\n\n";
# So can $jarich
$jarich->say_hello;
$jarich->praise_languages;
This results in the following output:
这导致以下输出:
Hello, my name is Paul Fenwick
I enjoy programming in: Perl C JavaScript
----
Hello, my name is Jacinta Richardson
I enjoy programming in: Perl C Haskell
If I want I can have as many Hacker objects as I like, but there's still only one Hacker class that describes how all of these work.
如果我想要我可以拥有尽可能多的黑客对象,但仍然只有一个黑客类可以描述所有这些对象的工作原理。
All the best,
祝一切顺利,
Paul
#2
A class is a type (like "SUV"). An object is an instance of a class ("David's SUV").
类是一种类型(如“SUV”)。对象是类的实例(“David的SUV”)。
#3
Perl-wise:
- A class is a
package
--a specification. A set of behaviors and data mainly to aid those behaviors. - An object is typically a "hashref", that is a collection of specific data allowed by the behavior specification in the package (and inherited behaviors).
一个类是一个包 - 一个规范。一组行为和数据主要用于帮助这些行为。
对象通常是“hashref”,它是包中行为规范允许的特定数据的集合(以及继承的行为)。
Now, a hashref might hold a code reference. In most cases, that's behavior. But the only way the object could use that specific behavior is for that to be specified by some class behavior inherited (or mixed in) that expects that there might be a coderef sitting at that location and invoke it.
现在,hashref可能包含代码引用。在大多数情况下,这是行为。但是对象可以使用该特定行为的唯一方法是由一些继承(或混合)的类行为指定,该行为期望可能存在位于该位置并调用它的coderef。
#4
Another way to think of it is a class is a blueprint for how an object will be built.
另一种思考方式是类是如何构建对象的蓝图。
#5
Objects are single instances of a Class.
对象是类的单个实例。
#6
You are an object of class Human
你是人类的对象
(Classes in Perl are modules with some special qualities, you should better first understand only the general case).
(Perl中的类是具有一些特殊性质的模块,您最好先了解一般情况)。
#7
In perl class is nothing but it is a package name. It has a common code for the objects.
在perl类中,它只是一个包名。它有一个对象的公共代码。
object is a instance that has access the class's properties and methods.
object是一个可以访问类的属性和方法的实例。
package vehicle;
sub vehicle_detail
{
($number,$model,$num_of_wheel)=@_;
print "My car Details:\n@_";
}
The above class vehicle can be used by any vehicle such as bike,car,van..etc. The object is created by the operator bless.
上述级别的车辆可以被任何车辆使用,例如自行车,汽车,货车等。该对象由操作员保佑创建。
$bike_name='honda';
$ref_bike=\$bike_name;
bless $ref_bike,'vehicle';
Now the bless creates the object honda for the class vehicle.
现在,祝福为班级车辆创造了本田对象。
#8
I don't see people using the terms the same way in other languages. That may be one reason for the question. I think maybe PHP users say "class" when they should say "object", a lot of the time?
我不认为人们在其他语言中使用相同的术语。这可能是问题的一个原因。我想也许PHP用户在他们应该说“对象”时会说“课堂”,很多时候?
Anyway, what about this example -- imagine you had to create two different database connections for two different databases:
无论如何,这个例子怎么样 - 想象你必须为两个不同的数据库创建两个不同的数据库连接:
my $oracle_database_handle = DBI->connect( <oracle connection details here> );
my $mysql_database_handle = DBI->connect( <mysql connection details here> );
you would have created two objects for doing two different things, but they're both the same kind of thing -- DBI database connections.
你会创建两个对象来做两件事,但它们都是同一类东西--DBI数据库连接。