达内课程-面向对象简介
2017-07-11 09:31
197 查看
类
对事物、算法、逻辑、概念的抽象
相关的属性数据、运算方法封装在一个类组件中
对象(实例)
理解成从图纸生产的产品
每个对象占用独立的内存空间,保存各自的属性数据
每个对象可以独立控制,让它执行指定的方法代码
引用
理解成遥控器
引用变量中保存一个对象的内存地址,通过引用变量可以找到对象的内存空间,访问它的属性数据,或控制它执行方法
MainActivity
xml
FlashLight类
xml
FlashLight.java
MainActivity.java
xml
MainActivity
宠物狗类
Dog
xml
MainActivity
对事物、算法、逻辑、概念的抽象
相关的属性数据、运算方法封装在一个类组件中
对象(实例)
理解成从图纸生产的产品
每个对象占用独立的内存空间,保存各自的属性数据
每个对象可以独立控制,让它执行指定的方法代码
引用
理解成遥控器
引用变量中保存一个对象的内存地址,通过引用变量可以找到对象的内存空间,访问它的属性数据,或控制它执行方法
Soldier类
public class Soldier { //成员变量 int id;//默认0 int blood = 100; //成员方法 public void go(TextView tv){ tv.setText(id+"前进"+"\n"+tv.getText()); } public void attack(TextView tv){ if(blood == 0){ tv.setText("这是"+id+"号士兵的尸体"+"\n"+tv.getText()); return; } tv.setText(id+"号士兵进攻"+"\n"+tv.getText()); int b = new Random().nextInt(10); if(blood<b){ blood = b; } blood -= b; if(blood == 0){ tv.setText(id+"号士兵阵亡"+"\n"+tv.getText()); } } }
MainActivity
public class MainActivity extends AppCompatActivity { TextView textView; Soldier s1;//默认null Soldier s2;//默认null @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void doClick(View view) { textView = (TextView) findViewById(R.id.textView); switch (view.getId()) { case R.id.button1: 4000 f1(); break; case R.id.button2: f2(); break; case R.id.button3: f3(); break; } } public void f1(){ s1 = new Soldier(); s1.id = 9527; //用s1找到士兵对象内存空间 //访问它的属性变量id textView.setText("士兵9527已创建"+"\n"); } public void f2(){ s1.go(textView); } public void f3(){ s1.attack(textView); textView.setText("士兵"+s1.id+"血量"+s1.blood+"\n"+textView.getText()); } }
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="新建士兵1" android:onClick="doClick"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="前进" android:onClick="doClick"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="进攻" android:onClick="doClick"/> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="新建士兵2" android:onClick="doClick"/> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="前进" android:onClick="doClick"/> <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="进攻" android:onClick="doClick"/> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
FlashLight类
xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:id="@+id/layout" android:background="@android:color/black"> <ToggleButton android:id="@+id/toggleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="开" android:textOff="关" android:onClick="doClick" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="红" android:onClick="doClick"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="白" android:onClick="doClick"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="蓝" android:onClick="doClick"/> </LinearLayout>
FlashLight.java
public class FlashLight { //属性变量,成员变量 int color = Color.BLACK; boolean on = false; public void turnOn(){ on = true; } public void turnOff(){ on = false; } }
MainActivity.java
public class MainActivity extends AppCompatActivity { FlashLight flashLight = new FlashLight(); LinearLayout linearLayout; ToggleButton toggleButton; Button red; Button white; Button blue; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); linearLayout = (LinearLayout) findViewById(R.id.layout); toggleButton = (ToggleButton)findViewById(R.id.toggleButton); red = (Button) findViewById(R.id.button1); white = (Button)findViewById(R.id.button2); blue = (Button)findViewById(R.id.button3); } public void doClick(View view) { switch (view.getId()) { case R.id.button1: f1(); break; case R.id.button2: f2(); break; case R.id.button3: f3(); break; case R.id.toggleButton: f4(); break; } } public void f1(){ //用light变量找到手电筒对象的内存空间地址 //访问它的color变量 flashLight.color = Color.RED; show(); } public void f2(){ flashLight.color = Color.WHITE; show(); } public void f3(){ flashLight.color = Color.BLUE; show(); } public void f4(){ //判断开关指示按钮状态 if(toggleButton.isChecked()){ flashLight.turnOn(); }else{ flashLight.turnOff(); } show(); } public void show(){ //根据flashlight属性控制界面 if(flashLight.on){ linearLayout.setBackgroundColor(flashLight.color); }else{ linearLayout.setBackgroundColor(Color.BLACK); } } }
汽车类
Car.javapublic class Car { public String color; public String brand; public int speed; public void go(TextView tv){ tv.append("\n"+color+brand+"汽车以时速"+speed+"前进"); } public void stop(TextView tv){ tv.append("\n"+color+brand+"汽车停止"); } }
xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:columnCount="4" android:orientation="vertical" android:rowCount="6"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="创建汽车" android:onClick="doClick" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="go" android:onClick="doClick"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" < da09 span class="hljs-attribute">android:text="stop" android:onClick="doClick"/> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity { Car car; Button create; Button go; Button stop; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); create = (Button)findViewById(R.id.button1); go = (Button)findViewById(R.id.button2); stop = (Button)findViewById(R.id.button3); textView = (TextView)findViewById(R.id.textView); } public void doClick(View view) { switch (view.getId()) { case R.id.button1: f1(); break; case R.id.button2: f2(); break; case R.id.button3: f3(); break; } } private void f1(){ car = new Car(); //默认值如下 //color=""; //brand=""; //speed=0; car.color = "红色"; car.brand="BMW"; car.speed=80; textView.setText("汽车已创建"); } private void f2(){ car.go(textView); } private void f3(){ car.stop(textView); } }
宠物狗类
Dog
public class Dog { public String name;//名字 public int hungry;//饥饿度 public int happy;//快乐度 //喂食 public void feed(TextView textView){ if(hungry==0){ textView.setText(name+"吃饱了\n"+textView.getText()); return; } hungry-=10; textView.setText("给"+name+"喂食,hungry:"+hungry+"\n"+textView.getText()); } //玩 public void play(TextView textView){ if(hungry == 100){ textView.setText(name+"很俄了\n"+textView.getText()); return; } happy+=10; hungry+=10; textView.setText("陪"+name+"玩耍,happy:"+happy+",hungry:"+hungry+"\n"+textView.getText()); } //惩罚 public void punish(TextView textView){ happy-=10; textView.setText("打"+name+"的pp,"+name+"哭叫:汪,happy"+happy+"\n"+ textView.getText()); } }
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:columnCount="4" android:orientation="vertical" android:rowCount="6"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="创建宠物狗" android:onClick="doClick" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="play" android:onClick="doClick"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="feed" android:onClick="doClick"/> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="punish" android:onClick="doClick"/> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity { Dog dog; Button create; Button play; Button feed; Button punish; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); create = (Button)findViewById(R.id.button1); play = (Button)findViewById(R.id.button2); feed = (Button)findViewById(R.id.button3); punish = (Button)findViewById(R.id.button4); textView = (TextView)findViewById(R.id.textView); } public void doClick(View view) { switch (view.getId()) { case R.id.button1: f1(); break; case R.id.button2: f2(); break; case R.id.button3: f3(); break; case R.id.button4: f4(); break; } } private void f1(){ dog = new Dog(); dog.name = "蠢狗"; dog.hungry = 50; dog.happy = 50; textView.setText("电子狗已经创建"); } private void f2(){ dog.play(textView); } private void f3(){ dog.feed(textView); } private void f4(){ dog.punish(textView); } }
相关文章推荐
- 达内课程-面向对象之继承与重写
- 达内课程-面向对象之关键词
- 达内课程-面向对象之抽象类
- 达内课程-面向对象之对象创建过程和java数据传递
- 达内课程-面向对象之接口
- 达内课程-面向对象之内部类
- 达内课程-面向对象之构造方法和重载
- 达内课程-面向对象之多态
- 达内课程-PrintWriter
- [导入]视频游戏开发系列课程(2):基础编程概念及C#简介
- 1、课程名称:面向对象(基础)
- Google---机器学习速成课程(十二)- 神经网络简介 (Introduction to Neural Networks)
- 达内课程-android入门知识(下)
- 耶鲁大学开放课程:《古希腊历史简介》
- PHP 高级课程笔记 面向对象
- 达内课程-音乐播放器v1.0(下)
- 大一下学期的自我目标(要求包含对大一上学期的总结、对面向对象课程完成后学习到的能力的预期,对面向对象课程的期望、对编程和专业能力的愿景规划)
- JavaScript面向对象简介
- 计算机组成原理1.1.1 课程简介