您的位置:首页 > 编程语言 > Java开发

Java日记(5)— 面向对象程序设计简介/this的用法

2017-09-12 10:45 776 查看
代码一:

import java.text.*;
/*JDK在java.text包中,提供了对显示对象格式化的接口、类及异常处理,
* 这里我们只来谈一谈text包中的format类及其子类。
* 其中,最重要的是两个差不多互为“逆运算”的方法format(将某对象按设定格式化为字符串)与parse(将字符串反格式化为对象)。
*
* java.text.Format是一个抽象类,下面有三个子类:NumberFormat, DateFormat及MessageFormat,NumberFormat与DateFormat又是两个抽象类。
*/
public  class Example_5_2 {
public static void main(String[] args) {
Circle c1,c2;
c1 =new Circle(10);
Point p =new Point(9,8);
c2= new Circle(10,p);
c2.setRadius(40);
DecimalFormat df = new DecimalFormat("##,##0.00");
/*DecimalFormat是NumberFormat的子类;
* DecimalFormat类的作用是将数值型数据格式化成我们需要的格式的字符串或将字符串数据反格式化为一个Number型对象。
*#表示数字,0时不显示;0表示数字,0时显示
*/
System.out.println("c1 面积="+c1.getArea()+"\tc2 面积="+c2.getArea());
try {
System.out.println("C1 周长="+df.parse(df.format(c1.getCircumference()))+"\tc2的周长="+df.format(c2.getCircumference()));
} catch (ParseException e) {
// DecimalFormat.format方法:按照预定格式将对象转换成字符串
// DecimalFormat.format方法:按照预定的反格式化将字符还转换成对象
e.printStackTrace();
}
}

}
class Circle{
private Point p;
private float radius =0.0f;         //java允许这种写法,将radius的默认值主动改为0.0f;
public Circle(){}
public Circle(float r)              //构造方法
{
radius = r;
}
public Circle(float r,Point p)      //构造方法
{
this.p = p;
radius = r;
}
public void setRadius(float r){     //设定半径
radius = r;
}
public double getArea(){            //计算面积
final float PI = 3.14f;
float area;
area = PI* radius*radius;
return area;
}
public double getCircumference(){   //计算面积
final float PI = 3.14f;
float circumference;
circumference = 2* PI * radius;
return circumference;
}
}
class Point{
int x;
int y;
public Point(){
x=0;
y=0;
}
public Point(int x,int y){
this.x=x;
this.y=y;
}
}


this的用法(代表实例对象自身):

1.在实例方法和构造方法中使用this

代码:

public class Example_5_5 {

public static void main(String[] args) {
Cat garfield = new Cat("黄",12);
garfield.grow();
garfield.grow();
}
}

class Cat{
String furColor;
int height;
public Cat(String color){
this.furColor = color;      //通过this访问成员变量,this可以省略
this.cry();                 //通过this调用成员方法,this可以省略
}
public Cat(String color,int height)
{
this(color);                //通过this调用其它构造方法                //注意不能写成this.Cat(color);
this.height = height;       //通过this引用成员变量,this可以省略
}

public void cry(){
System.out.println("我是一只"+this.furColor+"颜色的猫");
}
public void grow(){
this.height++;//通过this访问成员变量,this可以省略
System.out.println("我长高了,身高为"+this.height);
}
}


2.区分成员变量和局部变量:

代码:

public class Example5_6 {
int x =188,y;
public static void main(String[] args) {
Example5_6 e =new Example5_6();
e.f();
}
void f(){
int x=3;
y=x;            //y的值是3
System.out.println("y="+y);

y=this.x;       //y的值是188
System.out.println("y="+y);
}
}


3.返回实例对象本身的引用

代码:

public class Example_5_7 {

public static void main(String[] args) {
Dog tom = new Dog();
System.out.println("新生的tom身高:"+tom.height+"cm");
tom=tom.grow();             //只写tom.grow()也可以
System.out.println("长大后的tom身高:"+tom.height+"cm");
}

}

class Dog{
int age;
float height;
public Dog(){
age =1 ;
height = 10;
}
public Dog grow(){
height=height +10;
age++;
return this;
}
}


4.使用this调用类的其他构造方法:

在类的构造方法中,可以使用this()来调用该类的其他构造函数,具体调用那个构造函数是根据this的参数类型决定的

public class Example_5_8 {

public static void main(String[] args) {
Animal a = new Animal(10,20);
System.out.println("新生小动物");
System.out.println("年龄="+a.age+"\t体重="+a.weight+"克\t 身高="+a.height+"cm");
}

}
class Animal{
int age;
String furColor;
String eyeColor;
String name;
float weight;
float height;

public Animal(float height)
{
this.age=1;
this.height=height;
}
public Animal(String name)
{
this.age=1;
this.name=name;
}
public Animal(float height,float weight)
{
his(height);                //调用第一个构造函数

this.weight = weight;
}
}


5.类方法(静态函数)中不可以使用this。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: