Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
githublock
java
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
java
/
javase
/
code
/
oopdemo2
/
src
/
com
/
mashibing
/
DogTest.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
39 lines (37 loc) · 1.59 KB
master
Breadcrumbs
java
/
javase
/
code
/
oopdemo2
/
src
/
com
/
mashibing
/
DogTest.java
Copy path
Top
File metadata and controls
Code
Blame
39 lines (37 loc) · 1.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package
com
.
mashibing
;
/**
* @author: 马士兵教育
* @create: 2019-08-24 21:03
*/
/*
* 封装:
* 概念:
* 将类的某些信息隐藏在类内部,不允许外部程序直接访问,
* 而是通过该类提供的方法来实现对隐藏信息的操作和访问
* 封装解决什么问题:
* 如果任何一个处理类都可以直接对Dog进行赋值操作,那么当值不准确的时候,可以回产生额外的结果,
* 如何在赋值的同时添加一些逻辑判断呢?
* 封装可以解决此问题
* 作用:
* 使用封装可以保证数据的规范,不符合规范的数据将无法进行操作
* 好处:
* 1、隐藏类的内部实现细节
* 2、只能通过提供的方法进行访问,其他方法无法访问
* 3、可以根据需求添加复杂的逻辑判断语句
* 4、方便修改实现
* 面向对象的封装(狭义)可以用一句话概述: 为了保证数据安全和规范
* 将类中的属性设置为私有属性,提供共有的外部方法供程序进行调用,可以实现丰富的细节操作
* 广义的封装:
* 可以将完成特定功能的代码块封装成一个方法,供不同的程序进行调用
* */
public
class
DogTest
{
public
static
void
main
(
String
[]
args
) {
Dog
dog
=
new
Dog
();
// dog.name="大黄";
// dog.setAge(-20);
dog
.
setAge
(
20
);
// dog.color="yellow";
// dog.show();
System
.
out
.
println
(
dog
.
getAge
());
}
}
You can’t perform that action at this time.