Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #36669 > unrolled thread
| Started by | parmenides <xparmenides@yahoo.com> |
|---|---|
| First post | 2017-06-18 20:02 +0800 |
| Last post | 2017-06-19 13:40 -0700 |
| Articles | 8 on this page of 68 — 10 participants |
Back to article view | Back to comp.lang.java.programmer
How to assign an object to another in it method? parmenides <xparmenides@yahoo.com> - 2017-06-18 20:02 +0800
Re: How to assign an object to another in it method? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-06-18 08:09 -0400
Re: How to assign an object to another in it method? Robert Klemme <shortcutter@googlemail.com> - 2017-06-18 15:31 +0200
Re: How to assign an object to another in it method? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-06-19 10:22 -0400
Re: How to assign an object to another in it method? Eric Douglas <e.d.programmer@gmail.com> - 2017-06-19 08:13 -0700
Re: How to assign an object to another in it method? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-06-19 11:53 -0400
Re: How to assign an object to another in it method? Eric Douglas <e.d.programmer@gmail.com> - 2017-06-19 09:06 -0700
Re: How to assign an object to another in it method? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-06-19 13:56 -0400
Re: How to assign an object to another in it method? lewbloch@gmail.com - 2017-06-27 13:49 -0700
Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-06-28 12:20 +0000
Re: How to assign an object to another in it method? Eric Douglas <e.d.programmer@gmail.com> - 2017-06-28 05:51 -0700
Re: How to assign an object to another in it method? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-06-28 14:03 -0400
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-02 21:23 -0400
Re: How to assign an object to another in it method? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-03 10:59 +0000
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-03 08:38 -0400
Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-03 14:04 +0000
Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-03 10:39 -0400
Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-03 16:28 +0000
Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-03 16:52 -0400
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-03 22:04 -0400
Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-03 23:15 -0400
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-04 21:46 -0400
Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-04 22:18 -0400
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-08 21:13 -0400
Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-08 22:12 -0400
Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-11 17:56 +0000
Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-11 15:40 -0400
Re: How to assign an object to another in it method? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-11 13:49 -0700
Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-11 17:41 -0400
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-11 20:27 -0400
Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-11 21:43 -0400
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-11 21:49 -0400
Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-12 17:20 -0400
Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-13 10:08 +0000
Re: How to assign an object to another in it method? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-13 11:35 +0000
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-13 08:14 -0400
Re: How to assign an object to another in it method? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-13 14:29 +0000
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-13 13:47 -0400
Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-13 18:09 +0000
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-13 15:01 -0400
Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-13 19:19 +0000
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-13 15:39 -0400
Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-13 16:49 +0000
Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-13 10:24 -0400
Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-13 17:19 +0000
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-13 21:17 -0400
Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-14 06:40 +0000
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-14 14:26 -0400
Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-15 14:36 +0000
Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-14 12:10 -0400
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-14 14:40 -0400
Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-14 14:51 -0400
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-14 21:05 -0400
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-14 21:06 -0400
Re: How to assign an object to another in it method? lewbloch@gmail.com - 2017-07-14 14:44 -0700
Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-12 16:23 +0000
Re: How to assign an object to another in it method? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-12 10:12 -0700
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-11 20:16 -0400
Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-11 21:46 -0400
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-11 21:52 -0400
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-11 20:24 -0400
Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-11 21:49 -0400
Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-11 21:55 -0400
Re: How to assign an object to another in it method? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-03 12:37 +0000
Re: How to assign an object to another in it method? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-03 11:34 -0400
Re: How to assign an object to another in it method? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-03 17:28 +0000
Re: How to assign an object to another in it method? v_borchert@despammed.com (Volker Borchert) - 2017-06-19 17:13 +0000
Re: How to assign an object to another in it method? Eric Douglas <e.d.programmer@gmail.com> - 2017-06-19 13:40 -0700
Page 4 of 4 — ← Prev page 1 2 3 [4]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2017-07-11 20:24 -0400 |
| Message-ID | <ok3q7q$jk0$1@gioia.aioe.org> |
| In reply to | #36842 |
On 7/8/2017 10:12 PM, Jerry Stuckle wrote: > On 7/8/2017 9:13 PM, Arne Vajhøj wrote: >> On 7/4/2017 10:18 PM, Jerry Stuckle wrote: >>> And yes, >>> the derived class can change the behavior of the base class - either >>> with or without default values. But then that is part of the reason for >>> having a derived class, is it not? >> >> Actually not. >> >> Changing behavior is a very suspicious activity for a sub class. > > Not at all. One of the purposes of a derived class might be to limit > the base class, It should not. That would break "is a". X + something "is a" X is true. X - something "is a" X is false. > for instance. Take the relationships > > Person -> Employee -> Manager > > Manager is much more restrictive than Employee, which in turn is much > more restrictive than person. Manager has added functionality compared to Employee. It has added all the functionality to have a team of people. Employee extending Person is a the classic example in basic OOP of how not to use inheritance. In most cases Person is too high an abstraction to be useful, but in case you need it then it should be: Manager "is a" Employee Employee "is a" Role Person "has a" Role To allow a Person to change Role. >> Adding missing behavior or adding behavior is what derived classes >> are supposed to do. > > That may be *one* of the things a derived class can do. But one must be > careful - adding a behavior which is not normal to the base class > generally indicates an invalid relationship. No. Adding behavior is the norm. That is why Java uses "class C extends P" and not "class C shrinks P". Arne
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2017-07-11 21:49 -0400 |
| Message-ID | <ok3uvl$efr$1@jstuckle.eternal-september.org> |
| In reply to | #36852 |
On 7/11/2017 8:24 PM, Arne Vajhøj wrote: > On 7/8/2017 10:12 PM, Jerry Stuckle wrote: >> On 7/8/2017 9:13 PM, Arne Vajhøj wrote: >>> On 7/4/2017 10:18 PM, Jerry Stuckle wrote: >>>> And >>>> yes, >>>> the derived class can change the behavior of the base class - either >>>> with or without default values. But then that is part of the reason >>>> for >>>> having a derived class, is it not? >>> >>> Actually not. >>> >>> Changing behavior is a very suspicious activity for a sub class. >> >> Not at all. One of the purposes of a derived class might be to limit >> the base class, > > It should not. > > That would break "is a". > > X + something "is a" X is true. > > X - something "is a" X is false. > >> for instance. Take the relationships >> >> Person -> Employee -> Manager >> >> Manager is much more restrictive than Employee, which in turn is much >> more restrictive than person. > > Manager has added functionality compared to Employee. It has added > all the functionality to have a team of people. > > Employee extending Person is a the classic example in basic OOP > of how not to use inheritance. > > In most cases Person is too high an abstraction to be useful, but > in case you need it then it should be: > > Manager "is a" Employee > Employee "is a" Role > Person "has a" Role > > To allow a Person to change Role. > >>> Adding missing behavior or adding behavior is what derived classes >>> are supposed to do. >> >> That may be *one* of the things a derived class can do. But one must be >> careful - adding a behavior which is not normal to the base class >> generally indicates an invalid relationship. > > No. Adding behavior is the norm. > > That is why Java uses "class C extends P" and not "class C shrinks P". > > Arne That is correct. The derive class has a "is-a" relationship to the base class. The derived class is always a more specialized version of the base class. If the derived class had things which were not valid in the base class, the is-a relationship would not hold. For instance, a Manager can be a more specialized version of Employee - all Managers are Employees, but not all Employees are Managers. That does not mean you cannot add features to the derived class. But you cannot add features which would cause the base class to be invalid. -- ================== Remove the "x" from my email address Jerry Stuckle jstucklex@attglobal.net ==================
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2017-07-11 21:55 -0400 |
| Message-ID | <ok3vj0$o5g$3@gioia.aioe.org> |
| In reply to | #36857 |
On 7/11/2017 9:49 PM, Jerry Stuckle wrote: > On 7/11/2017 8:24 PM, Arne Vajhøj wrote: >> On 7/8/2017 10:12 PM, Jerry Stuckle wrote: >>> On 7/8/2017 9:13 PM, Arne Vajhøj wrote: >>>> Adding missing behavior or adding behavior is what derived classes >>>> are supposed to do. >>> >>> That may be *one* of the things a derived class can do. But one must be >>> careful - adding a behavior which is not normal to the base class >>> generally indicates an invalid relationship. >> >> No. Adding behavior is the norm. >> >> That is why Java uses "class C extends P" and not "class C shrinks P". > > That is correct. The derive class has a "is-a" relationship to the base > class. The derived class is always a more specialized version of the > base class. If the derived class had things which were not valid in the > base class, the is-a relationship would not hold. > > For instance, a Manager can be a more specialized version of Employee - > all Managers are Employees, but not all Employees are Managers. > > That does not mean you cannot add features to the derived class. But > you cannot add features which would cause the base class to be invalid. True. But adding features (unlike changing features or removing features) are very unlikely to break the base class as the base class can not use them (ignoring obscure reflection cases). Arne
[toc] | [prev] | [next] | [standalone]
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Date | 2017-07-03 12:37 +0000 |
| Message-ID | <ojddpq$5g5$1@dont-email.me> |
| In reply to | #36793 |
On Mon, 03 Jul 2017 10:59:12 +0000, Martin Gregorie wrote:
Hadn't checked/compiled the previous example, which of course should be:
public class Demo
{
private String name;
private int number;
private boolean prisoner;
public Demo(String name, int answer, boolean prisoner)
{
this.name = name;
this.number = number;
this.prisoner = prisoner;
}
public Demo(String name)
{
this(name, 6, true);
}
}
--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2017-07-03 11:34 -0400 |
| Message-ID | <ojdnve$a0t$1@dont-email.me> |
| In reply to | #36795 |
On 7/3/2017 8:37 AM, Martin Gregorie wrote:
> On Mon, 03 Jul 2017 10:59:12 +0000, Martin Gregorie wrote:
>
> Hadn't checked/compiled the previous example, which of course should be:
>
> public class Demo
> {
> private String name;
> private int number;
> private boolean prisoner;
>
> public Demo(String name, int answer, boolean prisoner)
> {
> this.name = name;
> this.number = number;
For some reason, NetBeans warns about this line ... :(
(Don't feel too bad, Martin: Some Days Are Just Like That,
and I've had plenty of them.)
--
esosman@comcast-dot-net.invalid
Twelve hundred ninety-seven days to go.
[toc] | [prev] | [next] | [standalone]
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Date | 2017-07-03 17:28 +0000 |
| Message-ID | <ojdurm$qpp$1@dont-email.me> |
| In reply to | #36798 |
On Mon, 03 Jul 2017 11:34:39 -0400, Eric Sosman wrote:
> On 7/3/2017 8:37 AM, Martin Gregorie wrote:
>> On Mon, 03 Jul 2017 10:59:12 +0000, Martin Gregorie wrote:
>>
>> Hadn't checked/compiled the previous example, which of course should
>> be:
>>
>> public class Demo {
>> private String name;
>> private int number;
>> private boolean prisoner;
>>
>> public Demo(String name, int answer, boolean prisoner)
>> {
>> this.name = name; this.number = number;
>
> For some reason, NetBeans warns about this line ... :(
>
> (Don't feel too bad, Martin: Some Days Are Just Like That,
> and I've had plenty of them.)
Interesting: it compiled with no errors, using javac 1.8.0_91, so I
extended it a bit to this so it actually did something:
public class Demo
{
private String name;
private int number;
private boolean prisoner;
public Demo(String name, int number, boolean prisoner)
{
this.name = name;
this.number = number;
this.prisoner = prisoner;
}
public Demo(String name)
{
this(name, 6, true);
}
public void dialogue()
{
System.out.println("You are now " +
(prisoner ? "a prisoner" : "free to leave"));
System.out.println("I, " + name + ", am a free man!");
System.out.println("You are number " + number);
}
public static void main(String[] args)
{
Demo d = new Demo("Patrick McGoohan");
d.dialogue();
}
}
... and promptly found the mistake in the first constructor (2nd argument
had 'answer' as it sname, causing this.number to be assigned to itself).
I assume that this is what NetBeans objected to?
Anyway, this is fixed in the version shown above and runs as expected.
BTW, I don't use IDEs at all, preferring to work with a bog-standard text
editor and a couple of command-line consoles.
--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
[toc] | [prev] | [next] | [standalone]
| From | v_borchert@despammed.com (Volker Borchert) |
|---|---|
| Date | 2017-06-19 17:13 +0000 |
| Message-ID | <oi90od$4jl$1@Gaia.teknon.de> |
| In reply to | #36669 |
parmenides wrote:
> Hi,
>
> Suppose that there is a class 'Foo' and an its instance called 'f1'.
> I want to assign 'f1' to another instance called 'f2' within a method
> invoked by f2. The code like this:
>
> Class Foo {
> ... ...
> public someMethod() {
> Foo f1 = new Foo();
> f1.readFromDB(); // Now I get an instance
> this.XXX = f1.getXXX();
> this.YYY = f1.getYYY();
> ... ...
> }
> }
> ... ...
> Foo f2 = new Foo();
> f2.someMethod();
>
> That is I want to read data from Database, and instanciate an object
> from these data. Then, the object is assigned to another object. In
> someMethod(), the feasible way is to assign the member variables one by
> one. But, I think this is cumbersome at all. So I try to do the
> assignment as a whole like this:
> this = f1;
> and write a clone() method to support this kind of assignment. However,
> owing to 'this' is just an reference to 'f2' rather than an object, the
> compiler does not agree. Is there any way to do assigment as a whole in
> this situation? Thanks.
Why do you want to do that?
You will then have two distinct object representing the same database
content. What if one of them is changed afterwards? Which of them will
be authoritative?
--
"I'm a doctor, not a mechanic." Dr Leonard McCoy <mccoy@ncc1701.starfleet.fed>
"I'm a mechanic, not a doctor." Volker Borchert <v_borchert@despammed.com>
[toc] | [prev] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2017-06-19 13:40 -0700 |
| Message-ID | <44b0db62-e5ef-4556-8efe-18b3fa5caece@googlegroups.com> |
| In reply to | #36669 |
On Sunday, June 18, 2017 at 8:02:47 AM UTC-4, parmenides wrote:
> and write a clone() method to support this kind of assignment. However,
> owing to 'this' is just an reference to 'f2' rather than an object, the
> compiler does not agree. Is there any way to do assigment as a whole in
> this situation? Thanks.
There are 2 ways to make a copy of an instance and any values contained within without referencing those values, clone it or serialize it. This will automatically copy all primitives by value, making no link to the original. Any class level object variables you'll have to copy yourself. If you do the serialize method those objects will either be serializable or you'll have to create a new instance and copy them one property at a time. When using automatic copy any property you don't want copied is declared as transient.
One clone sample:
public class TestClone implements Cloneable {
private int var1;
public void setVar1(final int value) {
var1 = value;
}
public int getVar1() {
return var1;
}
public TestClone clone() throws CloneNotSupportedException {
return (TestClone) super.clone();
}
protected static void testClone() throws CloneNotSupportedException {
TestClone clone1 = new TestClone();
clone1.setVar1(5);
TestClone clone2 = (TestClone) clone1.clone();
System.out.println(clone2.getVar1());
clone2.setVar1(6);
System.out.println(clone1.getVar1());
}
public static void main(final String[] args) throws CloneNotSupportedException {
testClone();
}
}
Your basic serialize does this.
public static <T> T testSerialize(final T testObject) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
T endTestObject;
try {
final ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(testObject);
oos.close();
// deserialize
final byte[] streamingBytes = out.toByteArray();
System.out.println(streamingBytes.length + " bytes transferred");
final ByteArrayInputStream in = new ByteArrayInputStream(streamingBytes);
final ObjectInputStream ois = new ObjectInputStream(in);
endTestObject = (T) ois.readObject();
endTestObject.getClass();
} catch (final Exception e) {
System.out.println("TestSerialize Failed on " + testObject.getClass().toString());
return null;
}
return endTestObject;
}
Fun fact on serialize it converts your object to bytes, so you could use a socket connection to make a copy of it in a different JVM, assuming the 2 JVMs share the same class definition.
Both clone and serialize work by default on primitives and can be customize to copy or not copy exactly what you want copied.
[toc] | [prev] | [standalone]
Page 4 of 4 — ← Prev page 1 2 3 [4]
Back to top | Article view | comp.lang.java.programmer
csiph-web