java面向对象 04 方法,static修饰符,重载
2. 定义成员变量的操作就是说明变量或者创建对象操作。
-
成员方法:1. 方法时类的动态属性,标志了类所具有的的功能和操作,用来吧类和对象的数据封装在一起。
2. java的方法与其它语言中的函数或者过程类似,是一段完整功能的程序段。
-
static修饰符:1. 用static修饰符的成员变量就是静态成员变量(类变量)。
特点:是类的成员变量,不属于任何一个类的对象,不需要实例化该类就可以用。
保存在类的内存区成员变量的公共存储单元,(静态成员变量是一个公共的存储单元 ),
任何类的对象访问得到都是相同值,任何类对象修饰它,都是对同一个内存单元进行操作。
2. 没有用static修饰的变量是实例变量,必须实例化该类才可以使用实例化变量,
类方法只能使用类变量,不能使用实例变量。package demo05;
public class Perosn
{
/*
{
//代码块(匿名代码块)
}
static
{
//静态代码块
}
*/
{//2 :赋初始值
System.out.println("匿名代码块");
}
static//1 :只执行一次
{
System.out.println("静态代码块");
}
public Perosn()//3
{
System.out.println("构造方法");
}
public static void main(String[] args)
{
Perosn perosn1= new Perosn();
System.out.println("--------------");
Perosn perosn2= new Perosn();
}
}
package demo05;
public class Student
{
private static int age;//静态的变量 多线程!
private double score;//非静态的变量
public void run()
{
go();
}
public static void go()
{
}
public static void main(String[] args)
{
/* Student student=new Student();
//student.score;
//System.out.println(Student.score);
System.out.println(Student.age);
*/
new Student().run();
Student.go();
}
}
package demo05;
//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Text
{
public static void main(String[] args)
{
System.out.println(random());
System.out.println(PI);
}
}
-
成员方法:
方法的声明:方法由方法声明和方法体组成。
格式:[修饰符或者限定项] 返回值类型 方法名 (形式参数列表) [Throws异常列表]
{
方法体各语句;
}
方法不需要返回值时,则必须声明其返回类型为void。
-
this:1. this表示当前对象本身,更准确的说this代表当前对象的一个引用。
2. 对象的引用可以理解为对象的另一个名字,通过引用可以顺利的访问到对象,
包括访问,修改对象的成员变量,调用对象的方法。
-
方法的参数传递:1. 参数类型可以是简单引用类型(数组,类或接口)
2. 简单的数据类型实现的是值的传递。方法接收的是参数值,但不改变参数值。
3 .想改变参数的值,则使用引用数据类型,因为引用数据类型传递给该方法是数据在
class A
{
public static void main(String[] args)
{
int val=10;
PassTest passTest=new PassTest();
passTest.changeint(val);//简单类型
System.out.println(val);//10
passTest.ptvalue=10;
System.out.println(passTest.ptvalue);//10
passTest.changeObjValue(passTest);//引用数据类型
System.out.println(passTest.ptvalue);//99
}
}
class PassTest
{
int ptvalue;
public void changeint(int value)
{
value=55;
}
public void changeObjValue(PassTest ref)
{
ref.ptvalue=99;
}
}
方法体:
class A
{
public static void main(String[] args)
{
Variable variable=new Variable();
System.out.println("y="+variable.x+"y="+ variable.y+"z="+ variable.z);//结果:0,0,0
variable.init(20,30);
System.out.println("y="+variable.x+"y="+ variable.y+"z="+ variable.z);//结果:20,30,0
}
}
class Variable
{
int x=0,y=0,z=0;
void init(int x,int y)
{
this.x=x;
this.y=y;
int z=5;//局部变量
System.out.println(x+""+y+""+z);//结果:20,30,5
}
}
//如果不用this当前对象时,类的成员变量会被隐藏。
-
方法重载:1.方法重载多少个方法可以享有相同的名字。但是这些方法的参数必须不同,
或者是参数类型个数不同, 或者是参数类型不 同。
2. 编译器根据承参数个数和类型来决定当前所调用的方法,如果两个方法的参数类型和个数相同,
只有 返回类型不同,编译会出错。
3.重载没有减少编程工作量,但能使编程实现方法变得简单,只需要记住一个方法名。

class MethodOverloading
{
void receive(int i)
{
System.out.println("Receive one int data");
System.out.println("i="+i);
}
void receive(int x,int y)
{
System.out.println("Receive two int datum");
System.out.println("x="+x+" y="+y);
}
void receive(double d)
{
System.out.println("Receive one double data");
System.out.println("d="+d);
}
void receive(String s)
{
System.out.println("Receive a string");
System.out.println("s="+s);
}
}
public class MethodOverloadingTest
{
public static void main(String args[])
{
MethodOverloading mo=new MethodOverloading();
mo.receive(1);
mo.receive(2,3);
mo.receive(12.56);
mo.receive("very interesting,isn't it?");
}
}
浙公网安备 33010602011771号