对象的组合
组合与复用
如果一个对象a组合了对象b,那么对象a就可以委托对象b,调用对象b调用其方法,即对象a以组合的方式复合对象b的方法
Circle.java
public class Circle {
double radius,area;
void setRadius(double r){
radius=r;
}
double getRadius(){
return radius;
}
double getArea(){
area=3.14*radius*radius;
return area;
}
}
Circular.java
public class Circular {
Circle bottom;
double height;
void setBottom(Circle c){//设置圆锥的底是一个Circle对象
bottom=c;
}
void setHeight(double h){
height=h;
}
double getVolume(){
if (bottom==null)
return -1;
else
return bottom.getArea()*height/3.0;
}
double getBottomRadius(){
return bottom.getRadius();
}
public void setBottomRadius(double r){
bottom.setRadius(r);
}
}
类的关联关系和依赖关系的UML图
关联关系
如果A类中的成员变量是用B类声明的对象,那么A和B的关系是关联关系,称A类的对象关联与B类的对象或A类的对象组合了B类的对象
依赖关系
如果A类中某个方法的参数是用B类声明的对象或某个方法返回的数据类型是B类对象,那么A和B的关系是依赖关系,称A依赖于B
实例成员与类成员
实例变量和类变量的声明
类体中包括成员变量的声明和方法的定义,而成员变量又可细分为实例变量和类变量(用关键字static 修饰的称作类变量,否则为实例变量)
class Dog {
float x;//实例变量
static int y;//类变量
}
public class Example {
// 实例变量
private int instanceVar;
// 类变量
private static int classVar;
public Example() {
// 实例变量的初始化
instanceVar = 10;
}
static {
// 类变量的静态初始化块
classVar = 20;
}
// 实例方法,操作实例变量
public void instanceMethod() {
instanceVar += 5; // 改变当前对象的instanceVar
}
// 静态方法,操作类变量
public static void classMethod() {
classVar += 5; // 改变所有对象共享的classVar
}
}
实例方法和类方法的定义
声明方法时,在方法类型前加关键字static修饰的是类方法,否则是实例方法
public class A {
int m;
static int n;
int max(int x, int y) { //实例方法
return x > y ? x : y;
}
void jerry() {
m = 12;
n = 90;
int r = max(m, n);
// 假设 speak 方法是 A 类的一部分,并且是静态的
speak("hello" + r);
}
// speak 方法是静态的,所以应该用类名调用,或者创建 A 的实例来调用
static void speak(String s) { //类方法
n = 100;
System.out.println(s);
System.out.println(n);
}
public static void main(String[] args) {
A a = new A(); // 创建 A 类的实例
a.jerry(); // 调用 jerry 方法
}
}
注:不能用static修饰构造方法
实例方法和类方法的区别
1、在实例方法中不仅可以操作实例变量,也可以操作类变量
2、类方法不可以操作实例变量,这是因为之前实例成员变量还没有分配内存
3、对于static方法,不必创建对象就可以用类名直接调用(创建对象会导致类中的实例变量被分配内存空间)
public class Example4_11 {
// 实例变量,属于对象
private int instanceVar;
// 类变量,属于类
private static int classVar;
// 实例方法,需要通过对象来调用
public int getInstanceVar() {
return instanceVar;
}
// 实例方法,可以访问实例变量
public void setInstanceVar(int instanceVar) {
this.instanceVar = instanceVar;
}
// 类方法,可以直接通过类名调用
public static int getClassVar() {
return classVar;
}
// 类方法,可以修改类变量
public static void setClassVar(int classVar) {
Example4_11.classVar = classVar;
}
// 主方法,用于程序的入口
public static void main(String[] args) {
// 创建Example类的实例
Example4_11 example1 = new Example4_11();
Example4_11 example2 = new Example4_11();
// 通过对象调用实例方法
example1.setInstanceVar(10);
example2.setInstanceVar(20);
// 通过对象访问实例变量
System.out.println("Instance variable of example1: " + example1.getInstanceVar());
System.out.println("Instance variable of example2: " + example2.getInstanceVar());
// 直接通过类名调用类方法
Example4_11.setClassVar(30);
// 直接通过类名访问类变量
System.out.println("Class variable: " + Example4_11.getClassVar());
}
}
方法重载
方法重载的语法规则
在一个类中可以有多个方法具有相同的名字,但这些方法的参数必须不同(参数个数不同或参数类型不同或都不同)
class People{
float hello(int a,int b){
return a+b;
}
float hello(long a,int b){
return a-b;
}
double hello(double a,int b){
return a*b;
}
}
public class Example4_12 {
public static void main(String[] args) {
People tom=new People();
System.out.println("10+10="+tom.hello(10,10));
System.out.println("10-10="+tom.hello(10l,10));
System.out.println("10*10="+tom.hello(10.0,10));
}
}
注:构造方法也可以重载,但构造方法不参与和非构造方法之间的重载比较
this关键字
在构造方法中使用this
当关键字this出现在类的构造方法中,代表构造方法所创建的对象
public class Eeample14 {
int leg,hand;
String name;
People(String s){
name=s;
this.init(); //可以省略this
}
void init(){
leg=2;
hand=2;
System.out.println(name+"有"+hand+"只手"+"有"+leg+"条腿");
}
public static void main(String args[]){
People bush= new People();
//在创建bush中的this就是对象bush
}
}
在实例方法中使用this
当this关键字出现在实例方法中时,this就代表正在调用该方法的当前对象
实例方法可以操作类的成员变量,当实例成员变量在实例方法中出现是时,默认的格式是:
this.成员变量
当static成员变量在实例方法中出现时,默认格式是:
类名.成员变量
public class Example4_9_2 {
public static void main(String []args){
class A{
int x;
static int y;
void f(){
this.x=100;
A.y=200;
}
}
}
}
通常情况下,可以省略实例成员变量名字前的"this."以及static变量前面的"类名."
但是,当实例或static成员变量的名字和局部变量的名字相同时,成员变量前面的"this."或“类名.”不可以省略
实例方法可以调用类的其他方法,对于实例方法,调用的默认格式是;
this.方法;
对于类方法,调用的默认格式是;
类名.方法;
class f{
this.g();
b.h();
}
void g(){
System.out.println("ok");
}
static void h(){
System.out.println("hello");
}
包
包语句
通过关键字package声明包语句,package语句的一般格式是:
package 包语句
注:在源程序中最多有一个包语句
包名可以是一个合法的标识符,也可以是若干个标识符加" . "分隔而成,例如:
package sunrise
或
package sun.com.cn;
有包名的类的存储目录
如果一个类有包名,那么就不能在任意位置存放,否则虚拟机将无法加载这样的类
运行有包名的主类
package tom.jiafei;
public class Example4_15 {
public static void main(Str arg[]){
Student sty=new = Student(10201);
stu.speak();
System.out.println("主类的包名也是 tom.jiafei");
}
}
访问权限
什么是访问权限
访问限制修饰符有public,private,protected
私有变量和私有方法
用关键字private修饰的成员变量和方法称为私有变量和私有方法
class Tom{
private float weight;//私有变量
private float f(float a,float b){//私有方法
return a+b;
}
}
在另外一个类中,使用类Tom创建对象后,该对象不能访问自己的私有变量,调用类中的私有方法,例如:
class Jerry{
void g(){
Tom cat =new Tom();
cat.weight=23f;//违法
float sun=cat.f;//违法
}
}
class Student{
private int age;
public void setAge(int age ){
if(age>=7&&age<=28){
this.age=age;
} }
public int getAge(){
return age;
}
}
public class Example4_15 {
public static void main (String arg[]){
Student zhang=new Student();
Student geng=new Student();
zhang.setAge(23);
System.out.println("zhang 的年龄是"+zhang.getAge());
geng.setAge(25);
//"zhang.age=23;"或"geng.age=25;"都是非法的
System.out.println("geng的年龄:"+geng.getAge());
}
}
共有变量和共有方法
用关键字public修饰的成员变量和方法称为私有变量和私有方法
class Tom{
public float weight;//私有变量
public float f(float a,float b){//私有方法
return a+b;
}
}
class Jerry{
void g(){
Tom cat =new Tom();
cat.weight=23f;//合法
float sum=cat.f(3,4);//和法
}
}
友好变量和友好方法
不用关键字public,private,protected修饰的成员变量和方法称为友好变量和友好方法
受保护的成员变量和方法
用关键字protected修饰的成员变量和方法称为私有变量和私有方法
class Tom{
protected float weight;//私有变量
protected float f(float a,float b){//私有方法
return a+b;
}
}
当另外一个类中使用了Tom类创建了一个对象后,如果这个类与Tom类在同一个包中,那么该对象可以使用Tom类中的protected变量个protected方法
注:在另外一个类中使用pubic类创建的对象要保证他们在同一个包中
访问限制修饰符按访问权限从高到低的顺序是:public->protect->友好的->private
基本类型的类封装
可以对基本类型类的对象进行四则运算,例如
public class Example {
public static void main(String[] args){
int number1 =100;
double number2=6.18;
Float number3=3.14F;
double result=number1+number2+number3;
System.out.println(result);
}
}
Var局部变量
在类和类体重,不可以使用Var声明成员变量,即仅限于在方法体内使用var声明局部变量
使用var声明局部变量时,必须显式的指定初值(初值不可以是null)
import java.util.Date;
class Tom{
void f(double m){
var width=108; //var声明变量width并判断出是int型
var height=m;//var声明变量height并判断出是double型
var data=new Date();//var声明变量double并判断出是Date型
System.out.printf("%d,%f,%s",width,height,data);
}
}
public class Example4_14 {
public static void main(String args[]){
var tom=new Tom();
tom.f(6.18);
}
}
对象数组
class student{
int number;
}
public class Example4_22 {
public static void main(String args[]){
student stu[]=new student[10];//创建对象数组stu
for (int i=0;i<stu.length;i++){
stu[i]=new student();
stu[i].number=101+i;
}
for(int i=0;i< stu.length;i++){
System.out.println(stu[i].number);
}
}
}
若有侵权,请联系作者