Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
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 }}
ChinnYu
Java_note
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Java_note
/
Lesson4_4.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
47 lines (41 loc) · 1012 Bytes
master
Breadcrumbs
Java_note
/
Lesson4_4.java
Copy path
Top
File metadata and controls
Code
Blame
47 lines (41 loc) · 1012 Bytes
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
40
41
42
43
44
45
46
47
/**
封裝性
如果屬性沒有封裝,那麼在本類之外創建對象後,可以直接訪問屬性
private 關鍵字:訪問權限修飾符 ,public 表示公有的 private表示私有的屬性或方法 ,
只能在本類中訪問,公有的屬性和方法,可以被類外部的其他類訪問,想要在類外部訪問
私有屬性,我們需要提供公有方法來間接訪問
通常在一個類中,屬性都私有化,並對外提供getter and setter方法
*/
public class Lesson4_4{
public static void main(String[] args){
Person p1= new Person();
//p1.name = "飛飛";
//p1.age = 16;
p1.setName("飛飛");
p1.setAge(16);
}
}
//沒有方裝之前
class Person{
//屬性的封裝
private String name;//成員變量,類中定義
private int age;//成員變量,類中定義
//getter and setter
//對外提供一個為name屬性設值的方法
public void setName(String name){
this.name = name;
}
//對外提供一個獲取name屬性的方法
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public void run(int len){//參數也是局部變量
int m = len;//m也是局部變量
}
}
You can’t perform that action at this time.