📄 16.txt
字号:
//
软件开发的六个阶段
需求分析
系统设计
编码实现
测试阶段
反复阶段
系统维护
//
声明类
语法格式:
[< modifiers>] class < class_name> {
[<attribute_declarations>]
[<constructor_declarations>]
[<method_declarations>]
}
举例:
public class Person{
private int age ;
public void showAge(int i) {
age = i;
}
}
声明属性
语法格式:
[< modifiers>] type < attr_name> [=defaultValue] ;
举例:
public class Person{
private int age;
public String name = “Lila”;
}
声明方法
语法格式:
< modifiers> <return_type> <name>([< argu_list>]) {
[< statements>]
}
举例:
public class Person{
private int age;
public int getAge() { return age; }
public void setAge(int i) {
age = i;
}
}
//
面向对象的应用举例
程序BirthDate.java : //定义类BirthDate
public class BirthDate {
private int day;
private int month;
public int year;
public void setDay(int d) { day = d; }
public void setMonth(int m) { month = m; }
public void setYear(int y) { year = y; }
public int getDay() { return day; }
public int getMonth() { return month; }
public int getYear() { return year;}
public void show(){
System.out.println(day + "-" + month + "-" + year);
}
}
//
信息的封装和隐藏
Java中通过将数据封装、声明为私有的(private),再提供一个或多个公开的(public)方法实现对该属性的操作,以实现下述目的:
隐藏一个类的实现细节;
使用者只能通过事先定制好的方法来访问数据,可以方便地加入控制逻辑,限制对属性的不合理操作;
便于修改,增强代码的可维护性;
//
构造方法
功能:创建其所属类型的一个新的对象。
语法格式:
< modifiers> <class_name>([< argu_list>]) {
[< statements>]
}
举例:
public class Person {
private int age;
public Person() { age = 18; }
public Person(int i) { age = i; }
public void setAge(int i) { age = i; }
}
//
Java语言中,每个类都至少有一个构造方法;
如果类的定义者没有显式的定义任何构造方法,系统将自动提供一个默认的构造方法:
默认构造方法没有参数
默认构造方法没有方法体
Java类中,一旦类的定义者显式定义了一个或多个构造方法,系统将不再提供默认的构造方法;
//
为便于管理大型软件系统中数目众多的类,解决类的命名冲突问题,Java引入包(package)机制,提供类的多重类命名空间。
//
为使用定义在不同包中的Java类,需用import语句来引入所需要的类
//
// 得到系统属性,并输出到文件
import java.util.Properties;
import java.util.Enumeration;
import java.io.*;
public class PropertyTest {
public static void main(String[] args)
{
Properties ps = System.getProperties();
Enumeration pn = ps.propertyNames();
ToFile file = new ToFile();
file.openFile("property.txt");
while ( pn.hasMoreElements() )
{
String pName = (String) pn.nextElement();
String pValue = ps.getProperty(pName);
file.writeFile(pName + "----" + pValue);
}
file.closeFile();
}
}
//
class ToFile
{
//private File f;
private FileWriter fin ;
private PrintWriter out ;
public void openFile(String s)
{
//f = new File(s);
try
{
fin = new FileWriter (s);
}
catch(IOException e)
{
System.out.print("File out!");
}
out = new PrintWriter (fin);
}
public void writeFile(String s)
{
out.println(s);
}
public void closeFile()
{
out.close();
}
}
///
// 获得系统字体类型
import java.awt.*;
public class ShowFont
{
public static void main(String[] args)
{
String[] Fontnames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for(int i = 0;i<Fontnames.length;i++)
{
System.out.println(Fontnames[i]);
}
/// 创建一个字体实例
Font font = new Font(Fontnames[1],Font.BOLD,14);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -