达内课程-面向对象之构造方法和重载
来源:互联网 发布:手机查淘宝消费总金额 编辑:程序博客网 时间:2024/04/30 07:06
构造方法
1、新建实例时执行的特殊方法
new Soldier();
new FlashLight():
new Car():
new Dog();
新建实例的同时,调用一个构造方法
2、一个类必须有构造方法
3、如果不写构造方法,编辑器编译代码时,会默认添加一个构造方法,如下:
public class A(){ //添加的默认无参空方法,没有返回类型 public A(){ }}
4、构造方法重载(具有不同参数)
增加程序的灵活性
public class A(){ public A(int i){ ... } public A(double d){ ... } public A(int i,double d){ ... }}
创建实例时,就不能写new A();
因为已经写了构造参数,运行时就不会再添加默认的构造方法
5、构造方法的作用,通常用来给成员变量赋值
举例1,给Dog类添加构造方法
public class Dog { public String name;//名字 public int hungry;//饥饿度 public int happy;//快乐度 //构造方法 public Dog(){ Log.d("Dog","执行了构造方法"); } //喂食 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()); }......
那么在创建Dog类时,会执行Log输出
举例2,给Dog类添加构造方法
public class Dog { public String name;//名字 public int hungry;//饥饿度 public int happy;//快乐度 //构造方法 public Dog(String name,int hungry,int happy){ //如果局部变量与成员变量同名,必须用this.xx来引用成员变量,如果不同名,可以直接写 this.name = name; this.happy = hungry; this.happy = happy; }......
创建Dog对象的时候写法
dog = new Dog("蠢狗",50,50);
举例3,给Dog类添加构造方法
public class Dog { public String name;//名字 public int hungry;//饥饿度 public int happy;//快乐度 //构造方法 public Dog(){ } public Dog(String name,int hungry,int happy){ //如果局部变量与成员变量同名,必须用this.xx来引用成员变量 this.name = name; this.happy = hungry; this.happy = happy; } ......
创建Dog对象的时候写法有两种
dog = new Dog("蠢狗",50,50);
dog = new Dog();
练习
Student类
public class Student { public int id; public String name; public String gender; public int age; //添加构造方法 public Student(){ } public Student(int id,String name){ //减少代码重复,调用3个参数方法,没有的参数gender可以传默认值null /*this.id = id; this.name = name;*/ this(id,name,null); } public Student(int id,String name,String gender){ //减少代码重复,调用3个参数方法,没有的参数age可以传默认值0 /* this.id = id; this.name = name; this.gender = gender;*/ this(id,name,gender,0); } public Student(int id,String name,String gender,int age){ this.id = id; this.name = name; this.gender = gender; this.age = age; } public String toString(){ return "\n姓名:"+name+"\n性别"+gender+"\n年龄"+age+"\nid"+id; }}
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="student()" android:onClick="doClick" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="student(id,name)" android:onClick="doClick"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="student(id,name,gender)" android:onClick="doClick"/> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="student(id,name,gender,age)" 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 button1; Button button2; Button button3; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); create = (Button)findViewById(R.id.button1); button1 = (Button)findViewById(R.id.button2); button2 = (Button)findViewById(R.id.button3); button3 = (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(){ Student s = new Student(); textView.setText(s.toString()); } private void f2(){ Student s = new Student(9527,"张三"); textView.setText(s.toString()); } private void f3(){ Student s = new Student(9528,"lili","女"); textView.setText(s.toString()); } private void f4(){ Student s = new Student(9529,"苏菲","女",22); textView.setText(s.toString()); }}
this
1、引用当前对象成员
this保存当前对象的内存地址
用this可以访问当前对象内存空间,访问当前对象成员
有同名局部变量时,必须使用this.xx访问成员变量
2、构造方法间调用
this();
从一个构造方法调用另一个重载的构造方法,目的:减少代码重复
一般从参数少的方法调用参数多的方法
必须是首行代码
正确
public Student(int id,String name){ this(id,name,null); }
错误
public Student(int id,String name){ System.out.println("xx"); this(id,name,null); }
练习:求坐标点距离
Point类
public class Point { int x; int y; public Point(int x,int y){ this.x = x; this.y = y; } public String tostring(){ return "("+x+","+y+")"; } public double distance(){ return Math.sqrt(x*x+y*y); }}
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"> <EditText android:id="@+id/xEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="x"/> <EditText android:id="@+id/yEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="y"/> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="student()" 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 { Point point; EditText xEditText; EditText yEditText; Button button1; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); xEditText = (EditText) findViewById(R.id.xEditText); yEditText = (EditText) findViewById(R.id.yEditText); button1 = (Button) findViewById(R.id.button1); textView = (TextView) findViewById(R.id.textView); } public void doClick(View view) { switch (view.getId()) { case R.id.button1: f1(); break; } } private void f1() { String x = xEditText.getText().toString(); String y = yEditText.getText().toString(); Point point = new Point(Integer.parseInt(x),Integer.parseInt(y)); textView.setText(point.tostring()+"\n"+point.distance()); }}
方法重载Overload
同名不同参
练习:求两点之间距离
Point类
public class Point { int x; int y; public Point(int x,int y){ this.x = x; this.y = y; } public String tostring(){ return "("+x+","+y+")"; } public double distance(){ return Math.sqrt(x*x+y*y); } public double distance(Point p){ int dx = x-p.x; int dy=y-p.y; return Math.sqrt(dx*dx+dy*dy); }}
xml
不变
MainActivity
仅修改f1方法
private void f1() { int x = Integer.parseInt(xEditText.getText().toString()); int y = Integer.parseInt(yEditText.getText().toString()); Point point1 = new Point(x,y); textView.setText(point1.tostring()+"距远点距离"+point1.distance()+"\n"); Point point2 = new Point(new Random().nextInt(10),new Random().nextInt(10)); textView.append(point1.tostring()+"距离"+point2.tostring()+"\n"+point1.distance(point2)); }
阅读全文
0 0
- 达内课程-面向对象之构造方法和重载
- 面向对象--构造方法、重载和重写
- 达内课程-面向对象之关键词
- 达内课程-面向对象之接口
- Java学习之面向对象二-----方法的重载和构造方法
- 面向对象之字段,属性,类,实例,构造方法,重载
- 达内课程-面向对象之对象创建过程和java数据传递
- 达内课程-面向对象之继承与重写
- 达内课程-面向对象之抽象类
- java学习之旅35--面向对象_08_方法重载_构造方法重载
- 面向对象第五课,方法重载,构造方法的重载
- 面向对象和方法重载
- 达内课程-面向对象简介
- 达内课程-面向对象之多态
- 达内课程-面向对象之内部类
- 面向对象之构造方法
- Java面向对象02-方法的签名,重载,构造方法和引用类型数组
- 面向对象 参数传递 (方法 -- 构造方法--重载)
- Uncaught TypeError: window.showModalDialog is not a function 谷歌
- IOS App 启动优化
- Android Studio 自动生成 serialVersionUID
- java 实现递归实现tree
- linux与Windows共享文件samba
- 达内课程-面向对象之构造方法和重载
- Java 类图(UML)
- 复习一下快速排序(带非递归)
- Vim光标移动命令
- Java编程开发学习路线图(附所有免费课程+在线自测)
- CodeForces
- 栈ADT
- UVa815--Flooded--洪水
- bzoj4724 [POI2017]Podzielno 数论