2017Google Study Jams之L3面向对象编程终极篇

来源:互联网 发布:linux 系统版本 编辑:程序博客网 时间:2024/06/06 09:22

此次活动的举办方:Google Study Jams活动官网

我的博客(同步此次活动笔记):CSDN博客、我的简书
Google Developers

通过前面的学习我们都知道Android是由Java语言所编写的,所以L3主要介绍的就是Java语言的这种面向对象的编程,并且使用到Android中。

一、面向对象编程

1、方法

  • a、概念
    方法是用来完成一系列操作的,例如对对象的操作就是在方法中完成的,如果需要使用一个对象来完成相应的工作,就需要调用它所对应的方法。方法收到对象的信息,并进行处理的操作。

  • b、方法的创建

修饰符  返回值类型 方法名(参数) {      //方法体}例如:public void add(int num) {      System.out.printf("num的值是:" + num);}
  • c、修饰符

    • 1) public:声明方法为公共类型,那么这个方法在任何包里都能访问到,包括子类也可以访问到,其性质如同是任何人都可以乘坐公共汽车一样。

    • 2)private:声明方法为私有类型,表示除了本类之外任何类都不能访问到这个方法,其性质如同私家车一样,除了自己的家人,陌生人是不允许乘坐的。

    • 3)default:声明方法为默认类型,这个修饰符是默认的可以不写,这种修改符表示只能在同包下访问。

    • 4)protected:声明方法为保护类型,表示修饰的方法只能在同包下的类进行访问或者非同包下的子类进行访问。

访问权限级别:public > protected > default > private。

  • d、参数
    一个方法可以有参数也可以无参数,而且参数的个数不限。
public void add() {    //无参数方法}public void add(int num) {    //有参且有一个参数}
  • e、命名
    方法的命名规则和之前的局部变量命名规则一样,同样采用驼峰式命名规则,即第一个字母小写,后面的单词第一个字母大写。而且参数的命名规则与方法的命名规则一直。
public void addNum(int num) {}
  • f、返回值(数据类型)
    方法的返回值可有可无。如果没有返回值的话为void,如果有返回值的话可以为Java的基本数据类型。

2、Java的基本数据类型

Java的基本数据类型分为八种:

  • 整型:byte、short、int、long
  • 浮点型:float、double
  • 字符型:char
  • 布尔型:boolean

3、类和对象

  • 概念:类是Java的核心,所有的Java程序都是基于类的,它定义了对象的属性和行为。类是一种抽象的东西,描述的是一个物品的完整信息,比如房子和图纸的关系。在Java里,图纸就是类,定义了房子的各种信息,而房子是类的实体。

  • 类的定义和对象的创建
    定义一个类表示定义了一个功能模块。它的主要内容包括成员变量和成员方法,成员变来那个描述的是对象的属性,成员方法描述的是对象的行为。

  修饰符 class 类的名称 {       //类的成员变量       //类的方法  }  例如:  public class StudyJams{  //Study Jams论坛      private int mStudyTime; //在线时间      private void writeNote() {             // 写笔记      }  }

创建对象:

   SutdyJams studyJams = new StudyJams();

可以通过对象使用类里方法和全局变量。例如:

  studyJams.mStudyTime = 100; // 为成员变量赋值  studyJams.wirteNote(); //调用写笔记的方法

4、if/else条件语句的使用

if条件语句常用来对流程的控制,条件的判断等。

  • if语句的语法
  if(表达式1) {      // 满足条件1  } else if(表达式2){      // 满足条件2  } else {      // 前面的条件都不满足  }   例如:  if (3 > 1) {       //...  }
  • if语句用法举例
    • 简写形式:if…
  if(表达式) {      //方法体  }
  • 一般形式:if…else
  if(表达式) {      //方法体  } else {   //方法体   }
  • 完整形式:if…else if …else
  if(表达式1) {      //方法体  } else if(表达式2) {      //方法体   } else {      //方法体   }

二、Android的跳转和资源的引用

1、基础控件延伸

  • EditText 文本编辑框
<?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:orientation="vertical">    <EditText        android:id="@+id/edit_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="请输入论坛名称"/></LinearLayout>

android:hint属性为文本框的提示语,android:text则表示文本框的值。
在代码中获取文本框的值:

EditText editText = (EditText) findViewById(R.id.edit_text);String str = editText.getText().toString();//str即为文本框中输入的值
  • CheckBox 复选框
<?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:orientation="vertical">    <CheckBox        android:id="@+id/checkBox"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checked="true" //是否选中,默认未选中        android:text="课程学习"/>    <CheckBox         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="笔记分享"/>    <CheckBox         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="每日签到"/></LinearLayout>

CheckBox

监听事件:cb.isCheck();来判断是否选中

CheckBox cb = (CheckBox) findViewById(R.id.checkBox); //初始化cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {       @Override       public void onCheckedChanged(CompoundButton compoundButton, boolean b) {            Toast.makeText(MainActivity.this, "触发了选中事件", Toast.LENGTH_SHORT).show();       } });
  • ScrollView 垂直滑动组件

ScrollView就是一个可以滚动的View,且滚动的方向是垂直方向的。需要注意的是ScrollView中只允许出现一个子控件。

<?xml version="1.0" encoding="utf-8"?><ScrollView    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">        <CheckBox            android:id="@+id/checkBox"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:checked="true"            android:text="课程学习"/>        <CheckBox android:layout_width="wrap_content"                  android:layout_height="wrap_content"                  android:text="笔记分享"/>        <CheckBox android:layout_width="wrap_content"                  android:layout_height="wrap_content"                  android:text="每日签到"/>    </LinearLayout></ScrollView>

2、资源引用

Android的资源引用,需要在res文件夹的values文件夹下建立对应的资源文件,对应关系如下:

资源类型 在Java文件中 在Xml文件中 Image R.drawable.photo @drawable/photo String R.string.hello @string/hello Layout Xml File R.layout.activity_main @layout/activity_main ID R.id.text_view @+id/text_view Color R.color.red @color/red Dimen R.dimen.padding_left @dimen/padding_left

3、使用Intent跳转页面

Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。

下面举个简单的例子:从当前页面跳转到另一个页面:

  • 无传值跳转
Intent intent = new Intent(MainActivity.this, StudyJamsActivity.class);                 startActivity(intent); // 启动Activity  
  • 传值跳转(跳转到StudyJams页面,并携带学习时间的小时数)
//跳转Intent intent = new Intent(MainActivity.this, StudyJamsActivity.class);   intent.putExtra(EXTRA_TIME, time);              startActivity(intent); // 启动Activity  //接收int time = getIntent().getIntExtra(EXTRA_TIME, 0);// time就是由上个页面传递过来的值

使用Intent可以传递基本数据类型,集合,对象等数据类型。

好了,Google Study Jams的笔记到这里就结束了,最后祝贺各位顺利结业!

Congratulations to all!

0 0