第十八课,理解修饰符的作用。修饰符(private、protected、public)是封装的基础,用于控制外部程序对对象内部信息的访问权限。看看这个程序,该类Rectangle有width字段和height字段以及构造函数,在main()方法中创建一个对象。
public class Main
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle(100, 200);
}
}
class Rectangle
{
int width;
int height;
Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
int getHeight()
{
return this.height;
}
}
该类声明了方法getHeight(),并返回字段height
public class Main
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle(100, 200);
}
}
class Rectangle
{
int width;
int height;
Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
int getHeight()
{
return this.height;
}
}
在main()方法中调用rect的getHeight()方法并打印结果
public class Main
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle(100, 200);
System.out.println(rect.getHeight());
}
}
class Rectangle
{
int width;
int height;
Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
int getHeight()
{
return this.height;
}
}
在main()方法中访问rect的字段height并打印结果
public class Main
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle(100, 200);
System.out.println(rect.getHeight());
System.out.println(rect.height);
}
}
class Rectangle
{
int width;
int height;
Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
int getHeight()
{
return this.height;
}
}
现在在height字段之前写上关键字private
public class Main
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle(100, 200);
System.out.println(rect.getHeight());
System.out.println(rect.height);
}
}
class Rectangle
{
int width;
private int height;
Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
int getHeight()
{
return this.height;
}
}
现在显示Main类仍然有height字段的错误,private的修饰符使其无法被外部的类访问,删除height字段的println语句再运行程序
public class Main
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle(100, 200);
System.out.println(rect.getHeight());
}
}
class Rectangle
{
int width;
private int height;
Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
int getHeight()
{
return this.height;
}
}
该height字段被封装在类中,只能在这个类内部被访问
public class Main
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle(100, 200);
System.out.println(rect.getHeight());
}
}
class Rectangle
{
int width;
private int height;
Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
int getHeight()
{
return this.height;
}
}
现在在getHeight()方法之前添加public关键字,表明该方法可以从外部访问
public class Main
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle(100, 200);
System.out.println(rect.getHeight());
}
}
class Rectangle
{
int width;
private int height;
Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
public int getHeight()
{
return this.height;
}
}
标记字段width为private
public class Main
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle(100, 200);
System.out.println(rect.getHeight());
}
}
class Rectangle
{
private int width;
private int height;
Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
public int getHeight()
{
return this.height;
}
}
添加使用public修饰的getWidth()方法,并返回宽度值
public class Main
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle(100, 200);
System.out.println(rect.getHeight());
}
}
class Rectangle
{
private int width;
private int height;
Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
public int getWidth(){
return this.width;
}
public int getHeight()
{
return this.height;
}
}
在main()方法中调用rect的getWidth()方法并打印结果
public class Main
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle(100, 200);
System.out.println(rect.getHeight());
System.out.println(rect.getWidth());
}
}
class Rectangle
{
private int width;
private int height;
Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
public int getWidth(){
return this.width;
}
public int getHeight()
{
return this.height;
}
}
public class Main
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle(100, 200);
System.out.println(rect.getHeight());
System.out.println(rect.getWidth());
}
}
class Rectangle
{
private int width;
private int height;
Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
public int getWidth(){
return this.width;
}
public int getHeight()
{
return this.height;
}
}
版权声明:本文为AIDE教程网原创文章,转载请附上原文出处链接和本声明。
本文链接:https://www.aidemx.cn/1601.html
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
喜欢就支持以下吧
请登录后发表评论
注册
社交帐号登录