一、面对对象编程的三个特性
二、类声明
class 类名 { //class为关键字,用来定义类,类名要符合标识符规定 类体内容 }
三、类体
四、成员变量
class Factory { float [] a; Workman zhang; //zhang是Workman类声明的变量,即对象 } class Workman { double x; }
五、方法
class Tom { int x = 10,y; void f() { int x = 5; y = x+this.x; //y得到的值是15 } }
class A { int a; float b; a = 12; //非法,这是赋值语句(不是变量声明,只能出现在方法中) b = 12.56f; //非法 }
六、构造方法
class Point { int x,y; Point() { //是构造方法 x = 1; y = 1; } void Point(int a,int b) { //不是构造方法(该方法的类型是void) x = a; y = b; } int Point() { return 12; } }
七、创建对象
八、使用对象
九、对象的引用和实体
十、类与程序的基本结构
十一、参数传值
public void g(double a,int ... x)
public void method(int ... x,int y) //×错误,最后一个参数y不是可变参数x所代表的参数之一
public int getSum(int ... x) { int sum = 0; for(int param:x) { //for(声明循环变量:参数代表) sum = sum+param; } }
十二、对象的组合
十三、实例成员与类成员
class Dog { float x; //实例变量 static int y; //类变量 }
import java.util.*; //Arrays类在java.util包中 public class Example4_11 { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); int [] a = {12,34,9,23,45,6,45,90,123,19,34}; Arrays.sort(a); //将double类型数组按升序排序 System.out.println(Arrays.toString(a)); //返回a数组的字符串 System.out.println("输入整数,程序判断是否在数组中:"); int number = scanner.nextInt(); int index = Arrays.binarySearch(a,number); //判断number是否在数组a中,若是,返回索引;若不是,返回一个负数 if(index>=0) System.out.println(number+"和数组中索引为"+index+"的元素值相同"); else System.out.println(number+"不与数组中任何元素值相同"); } }
十四、方法重载
十五、this关键字
public class People { int leg,hand; String name; People(String s) { name = s; this.init(); //可以省略this,写成“init()” } void init() { leg = 2; hand = 2; System.out.println(name+"有"+hand+"只手"+leg+"条腿"); } public static void main(String args[]) { People boshi = new People("布什"); //创建boshi时,构造方法中的this就是对象boshi } }
this.方法 //“this.”可省略
类方法调用的格式为
类名.方法 //“类名.”可省略
十六、包
package sunrise; package sun.com.cn;
package tom.jiafei;
则存储文件的目录结构中须包含:...\tom\jiafei,并要将字节码文件保存在此目录中。
C:\1000> javac tom\jiafei\源文件
C:\1000> java tom.jiafei.主类名
十七、import语句
java.util.Date date = new java.util.Date();
十八、访问权限
十九、基本类型的类封装
Double(double num); //创建double类型对象 doubleValue(); //返回该对象含有的double型数据
Character(char c); charVaule();
二十、对象数组
Student [] stu; stu = new Student[10];
Student stu[] = new Student[10];
stu[0] = new Student();
二十一、JRE扩展与jar文件
Manifest-Version: 1.0
Class: moon.star.TestOne moon.star.TestTwo //包名+class文件名,以空格隔开
Created-By: 1.8 //jdk1.8
将此文件保存在包所在的目录中。(所有的冒号下面都要一个空格)
jar cfm Jerry.jar hello.mf moon\star\TestOne.class moon\star\TestTwo.class
若包中只有class文件,也可以如下使用jar命令
jar cfm Jerry.jar hello.mf moon\star\*.class
import moon.star.*; public class Use { public static void main(String args[]) { TestOne a = new TestOne(); a.fTestOne(); TestTwo b = new TestTwo(); b.fTestTwo(); } }
二十二、文档生成器
javadoc Example.java
查看这些文档可知道源文件中类的组成结构。
javadoc -d F:\gxy\book Example.java
二十三、静态块
class AAA { static { //静态块 System.out.println("我是AAA中的静态块!"); } } public class E3 { static { //静态块 System.out.println("我是最先被执行的静态块!"); } public static void main(String args[]) { AAA a = new AAA(); //AAA的字节码进入内存 System.out.println("我在了解静态(static)块"); } }
习题:
3.1
3.2
3.3
3.5
3.6
4
public class Test { public static void main(String args[]) { CPU cpu = new CPU(); HardDisk disk = new HardDisk(); PC pc = new PC(); cpu.setSpeed(2200); disk.setAmount(200); pc.setCPU(cpu); pc.setHardDisk(disk); pc.show(); } } class CPU { int speed = 0; void setSpeed(int m) { speed = m; } int getSpeed() { return speed; } } class HardDisk { int amount = 0; void setAmount(int m) { amount = m; } int getAmount() { return amount; } } class PC { CPU cpu; HardDisk HD; void setCPU(CPU c) { cpu = c; } void setHardDisk(HardDisk h) { HD = h; } void show() { System.out.println("硬盘速度为:"+cpu.getSpeed()); System.out.println("硬盘容量为:"+HD.getAmount()); } }
原文:https://www.cnblogs.com/araysel/p/9945377.html