Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Rails for Java Developers phần 3 docx
MIỄN PHÍ
Số trang
30
Kích thước
205.1 KB
Định dạng
PDF
Lượt xem
1061

Rails for Java Developers phần 3 docx

Nội dung xem thử

Mô tả chi tiết

DEFINING CLASSES 61

Note that some of the accessor method calls are prefixed with an object

reference (other.), and other accessor calls are not. Methods not prefixed

by an explicit object reference are invoked on this, the object whose

method is currently executing.

Here’s the Ruby marry:

def marry(other)

other.last_name = self.last_name = "#{self.last_name}-#{other.last_name}"

end

Since Ruby methods always return the last expression evaluated, writer

(setter) methods return the new value set. This means that multiple

setters can be chained together, as in other.last_name = self.last_name =

.... Note that the Ruby setter methods being called in marry are prefixed

by an object, either other or self. Ruby’s self is the equivalent of Java’s

this. The explicit use of self here is important. In Ruby, last_name="X" is

ambiguous. Depending on the context, this might mean “Create a local

variable named last_name with value "X"” or “Call the method last_name=,

passing the parameter "X".” Using self makes it clear that you want the

“method” interpretation.

If you forget to prefix a setter with self, you may create hard-to-find

bugs. Java does not suffer from this ambiguity, so be careful.

Creating Static Methods

Sometimes methods apply to a class as a whole, instead of to any par￾ticular instance of a class. In Java these are called static methods: static methods

public static String getSpecies() {

return "Homo sapiens";

}

In Ruby, these methods are called class methods: class methods

def self.species

"Homo sapiens"

end

For purposes of this book, we can pretend that Java static methods and

Ruby class methods are similar beasts.2

2. The Ruby story is actually a good bit more complex than this. Unlike Java meth￾ods, Ruby class methods are polymorphic. There are at least five alternate syntaxes

that you can use to define a Ruby class method. There is a good RubyGarden dis￾cussion on class methods at http://www.rubygarden.org:3000/Ruby/page/show/ClassMethods.

In addition, Ruby also has class variables, but we think you should avoid them. See

http://www.relevancellc.com/archives/2006/11.

DEFINING CLASSES 62

Download code/java_xt/src/Person.java

public class Person {

private String firstName;

private String lastName;

public Person(String firstName, String lastName) {

this.firstName = firstName;

this.lastName = lastName;

}

public String getFirstName() {

return firstName;

}

public void setFirstName(String firstName) {

this.firstName = firstName;

}

public String getLastName() {

return lastName;

}

public void setLastName(String lastName) {

this.lastName = lastName;

}

public String getFullName() {

return String.format("%s %s", firstName, lastName);

}

public void marry(Person other) {

String newLastName =

String.format("%s-%s", getLastName(), other.getLastName());

setLastName(newLastName);

other.setLastName(newLastName);

}

public static String getSpecies() {

return "Homo sapiens";

}

}

java_xt/src/Person.java

Figure 2.2: A Java Person

IDENTITY AND EQUALITY 63

Download code/rails_xt/samples/person.rb

class Person

def initialize(first_name, last_name)

@first_name = first_name

@last_name = last_name

end

attr_accessor :first_name, :last_name

def full_name

"#{first_name} #{last_name}"

end

def marry(other)

other.last_name = self.last_name = "#{self.last_name}-#{other.last_name}"

end

def self.species

"Homo sapiens"

end

end

rails_xt/samples/person.rb

Figure 2.3: A Ruby Person

2.7 Identity and Equality

Java distinguishes object identity and object equality. Object identity Object identity

asks the question “Are two objects at the same location in memory?”

Testing object identity is the responsibility of the runtime, which man￾ages memory. Object equality asks the question “Do two objects have Object equality

state that is semantically equivalent?” Testing object equality is the

responsibility of the implementer of a class. This short Java example

illustrates the difference:

Download code/java_xt/src/TestEquals.java

public void testEquals() {

String s1 = "Java rocks!";

String s2 = s1;

String s3 = new String("Java rocks!");

assertTrue(s1 == s2);

assertFalse(s1 == s3);

assertTrue(s1.equals(s3));

}

Tải ngay đi em, còn do dự, trời tối mất!