📄 society.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>"Society" of Objects</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function OpenPopup(mylink,width,height){
var props = 'scrollbars = yes,width=' + width + ',height=' + height + '\'';
window.open(mylink,"",props);//replace MyfileToBeOpened with path to the file to be displayed
}
</script>
</head>
<body bgcolor="#FFFFFF" background="../figs/whitgran.jpg">
<div align="center">
<p align="left"><font size="-1">© Dmitry Gakhovich</font></p>
<font color="#FF0000" size="+3"><strong>Objects & References</strong></font></div>
<p>The aim of the tutorial is to demonstrate some object programming concepts
using "natural" objects. Indeed, object-oriented approach models our
world - "everything is an object"! Supposedly, you know already what
is an object. And as you remember objects have attributes, or fields, and methods.</p>
<p>Here we'll create a Society object, which, in its turn, will hold several Person
objects (Think about the society to which we belong to. Is the society an object?
Sure it is. And each person in the society is an object with its own characteristics).
</p>
<p> Lets start from the Person class.</p>
<p align="center"><img src="fig0.gif" width="213" height="221"> <strong>Fig.1</strong></p>
<p>For the Person class definition code follow the link: <a href="Person.java.txt" target="_blank">Person.java</a>.</p>
<p>As you may see, the Person class may contain references to some other objects,
and some of them are of the Person type. OK, we just are modelling our society...</p>
<p>Now, imagine that we have created two Person objects, <font color="#990000" face="Courier New, Courier, mono">Ann_</font>
and <font color="#990000" face="Courier New, Courier, mono">Tom_</font>. Initially
all references are null, so we have to set the references we want manually.
For example, Ann and Tom (these are name attributes for our objects) are a wife
and a husband (in other words, partners): <font color="#990000" face="Courier New, Courier, mono">Ann_.setPartner(Tom_)</font>
and <font color="#990000">Tom_.setPartner(Ann_)</font>. That means their "partner"
fields will point ("refer") to corresponding Person objects:</p>
<p align="center"><img src="fig1.gif" width="517" height="208"> <strong>Fig.2</strong></p>
<p>If Ann and Tom have a son, Tim, the picture becomes more complex:</p>
<p align="center"><img src="fig2.gif" width="518" height="428"> <strong>Fig.3</strong></p>
<p>Now we have a bunch of references and several not null fields type of Person
in three objects. But still only three objects!<br>
<font color="#FF0000">Important:</font> if we have a not-null field of some
object type, this field is just a reference, not a separate or a "hidden"
object or something else.</p>
<p>Now it is a time to introduce our society. Probably, we can test our Person
class without a Society. But normally people live in a society, and the society,
in its turn, provides some functions as registration of our births and marriages,
teaching, protecting, etc. Our "model" society will be rather simple.
It has a list of Person objects and a number of methods to add persons to the
society, register them as a husband and wife, run census (to get information
about persons in our society!) and some more "convenience" methods:</p>
<p align="center"><img src="fig3.gif" width="239" height="129"> <strong>Fig.4</strong></p>
<p align="left">To see the code for the Society class follow the link: <a href="Society.java.txt" target="_blank">Society.java</a>.</p>
<p>Person objects are stored in an ArrayList, which is, of course, a separate
object that contains Person references (see UML diagram <a href = "javascript:OpenPopup('uml.html','550','400')">here</a>),
but for the sake of example we talk only about Persons and Society. For more
information on the ArrayList class click <a href = "javascript:OpenPopup('arraylist.html','500','400')">ArrayList</a>.
</p>
<p>To test our Person and Society classes you may run <a href="SocietyTest_1.java.txt" target="_blank">SocietyTest_1.java</a>
an <a href="SocietyTest_1.java.txt" target="_blank">SocietyTest_2.java</a>.
These are console applications. </p>
<p>First one creates a society with very simple scenario similar to figs 2 and
3. We have two person, girl and guy, they marry and have a baby ( the gender
is chosen randomly - 50/50). The scenario runs census twice: before they have
married and after they have child. Code is easy to follow. Try.</p>
<p>The second scenario is rather complex. First, we have initially six Persons,
three males and three females. Many things here happen randomly: </p>
<ul>
<li>One of males (random one) falls in love with some random female Person.</li>
<li>His love may be accepted or declined (50/50).</li>
<li>But in any case we have a happy end - finally one of our guys marries to
one of our girls (the process is random!...).</li>
<li>After they are married we get (or they get) a new Person (new Object in
our Society!). It may be girl or boy (randomly - 50/50!).</li>
</ul>
<p>And after that we run a census (only once - at the end).</p>
<p><strong>SocietyTest_2</strong> is also a console application. But here we have
a sequence of message boxes with pictures that introduce our heroes, e.g. Arni<img src="fig6.gif" width="78" height="74">,
and lead us through the events in our society <img src="fig7.gif" width="79" height="74">.
You'll like it!</p>
<p>The code in all classes contains extensive comments and hopefully you can understant
it. Do not worry about some "convenience" methods for playing music
or sequencing events.</p>
<p>To get all files to run SocietyTest_2.java you need <a href="resources.zip">resources.zip</a>.
It contains all source files, gifs and midi.</p>
<p align="center"><img src="fig4.gif" width="161" height="40"></p>
<hr>
<p><a href="../index.html">Home</a></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -