对象封装(5)-传值调用、内部类、不定长度变量、static

static:被声明为static的成员,不会让个别对象拥有,而是属于类。

package cc.openhome;
//增加了 import static的语法,可以在使用静态成员时少打几个字
import static java.lang.System.in;
import static java.lang.System.out;

public class Ball {
    double radius;
    static final double PI=3.14159;
    static double toRadius(double angdeg)
    {
        return angdeg*(Ball.PI/180);
    }
    public static void main(String args[]){
    Ball ball=new Ball();

    out.println(ball.toRadius(60));
     }
    }

如果声明为static,那么建立对象只有radius的值会改变。被声明为static的成员,是将类名称作为命名空间。通过类名称与”.”运算符,就可以取得static成员。

不定长度变量:

package cc.openhome;
//增加了 import static的语法,可以在使用静态成员时少打几个字
import static java.lang.System.in;
import static java.lang.System.out;
public class Ball{
   public static int sum(int...numbers)
    {
        int sum=0;
        for(int number:numbers){
        sum+=number;
        }
        return sum;

    }
public static void main(String[] args){

    Ball bell=new Ball();
    out.println(bell.sum());
}

}

内部类:在类中再定义类,这称为内部类,简言之就是类中定义类
方法中生成类:这通常是是辅助方法中推算之用,方法外无法使用,很少使用

class Some{
public void doSome(){
class Other{

}
}

方法中定义匿名内部类并直接实例化,这与类继承或 接口操作有关

class OO{
    Object o=new Object(){
public String toString(){
return "无聊的语法示范而已";
}
};
}

这个语法定义了一个没有名称得类,他继承Object类,并重新定义了ToDtring方法,new表示实例化这个没有名称得类。

传值调用:
注意要使用static方法时候,要是所用到的东西都不能出现this,最好改变类为static

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.openhome;

/**
 *
 * @author Administrator
 */
public class Ulitimate {



    public static void main(String[] args) {
        // TODO code application logic here
    Customer c1=new Customer("Justin");
    some(c1);
    System.out.println(c1.name);
    Customer c2=new Customer("Justin");
    other(c2);
    System.out.println(c2.name);
    }
    static class Customer{
    String name;
    Customer(String name){
   this.name=name;
    }
    }
   static void some(Customer c){
   c.name="John";}
   static void other(Customer c){
   c=new Customer("Bill");
   }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值