编程语言的几个发展阶段
面向机器语言
汇编语言,根据机器不同对应的命令也不同,不能跨平台
面向过程语言
c语言,不同的模块负责问题的不同部分
面向对象语言
Java、c++,不同对象及它的行为(封装、继承、多态)
类
类声明
class + 类名
类体
变量声明:属性
方法定义:行为
成员变量
类型
基本类型:整型、浮点型、字符型、逻辑类型。
引用类型:数组、对象、接口。
有效范围
成员变量有效性与在类中的位置无关。
编程风格
一行只声明一个变量
方法
方法头
方法类型 + 名称 + ( )
方法体
局部变量只在方法内有效(与声明位置相关),成员变量在整个类中有效(与声明位置无关)。
区分成员变量和局部变量
当成员变量与局部变量名字相同时,成员变量被隐藏(使用需this)。
局部变量没有默认值
构造方法与对象的创建
构造方法
构造方法没有类型
创建对象
对象的声明
类的名字 + 对象的名字;
为声明的对象分配变量
例子Example4_1程序代码
例子Example4_2程序代码
对象的内存模型
创建对象,就是指为对象分配变量,并获得一个引用,以确保这些变量由该对象来操作管理。
使用对象
对象操作自己的变量
对象.变量;
对象调用类中的方法
对象.方法;
体现封装
例子Example4_3程序代码以及运行结果
class xiyoujiRenwu {
float height,weight;
String head, ear;
void speak(String s) {
head="歪着头";
System.out.println(s);
}
}
public class Example4_3 {
public static void main(String args[]) {
xiyoujiRenwu zhubajie,sunwukong;
zhubajie = new xiyoujiRenwu();
sunwukong = new xiyoujiRenwu();
zhubajie.height=1.80f;
zhubajie.head="大头";
zhubajie.ear="一双大耳朵";
sunwukong.height=1.62f;
sunwukong.weight=1000f;
sunwukong.head="秀发飘飘";
System.out.println("zhubajie的身高:"+zhubajie.height);
System.out.println("zhubajie的头:"+zhubajie.head);
System.out.println("sunwukong的重量:"+sunwukong.weight);
System.out.println("sunwukong的头:"+sunwukong.head);
zhubajie.speak("俺老猪我想娶媳妇");
System.out.println("zhubajie现在的头:"+zhubajie.head);
sunwukong.speak("老孙我重1000斤,我想骗八戒背我");
System.out.println("sunwukong现在的头:"+sunwukong.head);
}
}
对象的引用和实体
例子Example4_4代码及运行结果
class point {
int x,y;
void setXY(int m,int n){
x = m;
y = n;
}
}
public class Example4_4 {
public static void main(String args[]) {
point p1, p2;
p1 = new point();
p2 = new point();
System.out.println("p1的引用:" + p1);
System.out.println("p2的引用:" + p2);
p1.setXY(1111, 2222);
p2.setXY(-100, -200);
System.out.println("p1的x,y坐标:" + p1.x + "," + p1.y);
System.out.println("p2的x,y坐标:" + p2.x + "," + p2.y);
p1 = p2;
System.out.println("将p2的引用值赋给p1后:");
System.out.println("p1的引用:" + p1);
System.out.println("p2的引用:" + p2);
System.out.println("p1的x,y坐标:" + p1.x + "," + p1.y);
System.out.println("p2的x,y坐标:" + p2.x + "," + p2.y);
}
}
对象的组合
例子Example4_8程序代码及运行结果
class Circle {
double radius,area;
void setRadius(double r) {
radius=r;
}
double getRadius() {
return radius;
}
double getArea(){
area=3.14*radius*radius;
return area;
}
}
class Circular {
Circle bottom;
double height;
void setBottom(Circle c) {
bottom = c;
}
void setHeight(double h) {
height = h;
}
double getVolme() {
if(bottom == null)
return -1;
else
return bottom.getArea()*height/3.0;
}
double getBottomRadius() {
return bottom.getRadius();
}
public void setBottomRadius(double r){
bottom.setRadius(r);
}
}
public class Example4_8 {
public static void main(String args[]) {
Circle circle = new Circle();
circle.setRadius(10);
Circular circular = new Circular();
System.out.println("circle的引用:"+circle);
System.out.println("圆锥的bottom的引用:"+circular.bottom);
circular.setHeight(5);
circular.setBottom(circle);
System.out.println("circle的引用:"+circle);
System.out.println("圆锥的bottom的引用:"+circular.bottom);
System.out.println("圆锥的体积:"+circular.getVolme());
System.out.println("修改circle的半径,bottom的半径同样变化");
circle.setRadius(20);
System.out.println("bottom的半径:"+circular.getBottomRadius());
System.out.println("重新创建circle,cirlce的引用将发生变化");
circle = new Circle();
System.out.println("circle的引用:"+circle);
System.out.println("但是不影响circular的bottom的引用");
System.out.println("圆锥的bottom的引用:"+circular.bottom);
}
}
实例成员与类成员
实例变量和变量的区别
1、不同的对象的实例变量互不相同。
2、所有对象共享变量。
3、通过类名直接访问类变量。
例子Example4_10程序代码及运行结果
···
class Lader {
double 上底,高;
static double 下底;
void 设置上底(double a) {
上底 = a;
}
void 设置下底(double b) {
下底 = b;
}
double 获取上底() {
return 上底;
}
double 获取下底() {
return 下底;
}
}
public class Example4_10 {
public static void main(String args[]) {
Lader.下底=100;
Lader laderOne=new Lader();
Lader laderTwo=new Lader();
laderOne.设置上底(28);
laderTwo.设置上底(66);
System.out.println("laderOne的上底:"+laderOne.获取上底());
System.out.println("laderOne的下底:"+laderOne.获取下底());
System.out.println("laderTwo的上底:"+laderTwo.获取上底());
System.out.println("laderTwo的下底:"+laderTwo.获取下底());
}
}
![](https://img2018.cnblogs.com/blog/1271708/201903/1271708-20190317173535048-527412312.png)
#方法重载
方法的返回类型和参数名字不参与比较,如果两个方法的名字相同,即使返回类型不同,也必须保证参数不同。
#this关键字
例子Example4_14程序代码及运行结果
![](https://img2018.cnblogs.com/blog/1271708/201903/1271708-20190317173604810-814537724.png)
#包
package + 包名;
#import语句
例子Example4_16程序代码及运行结果
![](https://img2018.cnblogs.com/blog/1271708/201903/1271708-20190317173729670-1026317522.png)
#对象数组
例子Example4_21程序代码及运行结果
![](https://img2018.cnblogs.com/blog/1271708/201903/1271708-20190317173801872-1093098220.png)
#应用举例
例子Example4_23程序代码及结果
class Rational {
int numerator = 1;
int denominator = 1;
void setNumerator(int a) {
int c=f(Math.abs(a),denominator);
numerator = a/c;
denominator = denominator/c;
if(numerator<0&&denominator<0) {
numerator = -numerator;
denominator = -denominator;
}
}
void setDenominator(int b) {
int c=f(numerator,Math.abs(b));
numerator = numerator/c;
denominator = b/c;
if(numerator<0&&denominator<0) {
numerator = -numerator;
denominator = -denominator;
}
}
int getNumerator() {
return numerator;
}
int getDenominator() {
return denominator;
}
int f(int a,int b) {
if(a==0) return 1;
if(a<b) {
int c=a;
a=b;
b=c;
}
int r=a%b;
while(r!=0) {
a=b;
b=r;
r=a%b;
}
return b;
}
Rational add(Rational r) {
int a=r.getNumerator();
int b=r.getDenominator();
int newNumerator=numeratorb+denominatora;
int newDenominator=denominatorb;
Rational result=new Rational();
result.setNumerator(newNumerator);
result.setDenominator(newDenominator);
return result;
}
Rational sub(Rational r) { //
int a=r.getNumerator();
int b=r.getDenominator();
int newNumerator=numeratorb-denominatora;
int newDenominator=denominatorb;
Rational result=new Rational();
result.setNumerator(newNumerator);
result.setDenominator(newDenominator);
return result;
}
Rational muti(Rational r) {
int a=r.getNumerator();
int b=r.getDenominator();
int newNumerator=numeratora;
int newDenominator=denominatorb;
Rational result=new Rational();
result.setNumerator(newNumerator);
result.setDenominator(newDenominator);
return result;
}
Rational div(Rational r) {
int a=r.getNumerator();
int b=r.getDenominator();
int newNumerator=numeratorb;
int newDenominator=denominatora;
Rational result=new Rational();
result.setNumerator(newNumerator);
result.setDenominator(newDenominator);
return result;
}
}
public class Example4_23 {
public static void main(String args[]) {
Rational r1 = new Rational();
r1.setNumerator(1);
r1.setDenominator(5);
Rational r2 = new Rational();
r2.setNumerator(3);
r2.setDenominator(2);
Rational result = r1.add(r2);
int a = result.getNumerator();
int b = result.getDenominator();
System.out.println("1/5+3/2 = " + a + "/" + b);
result = r1.sub(r2);
a = result.getNumerator();
b = result.getDenominator();
System.out.println("1/5-3/2 = " + a + "/" + b);
result = r1.muti(r2);
a = result.getNumerator();
b = result.getDenominator();
System.out.println("1/5*3/2 = " + a + "/" + b);
result = r1.div(r2);
a = result.getNumerator();
b = result.getDenominator();
System.out.println("1/5 / 3/2 = " + a + "/" + b);
int n = 10, k = 1;
System.out.println("计算2/1+3/2+5/3+8/5+13/8……的前" + n + "项和。");
Rational sum = new Rational();
sum.setNumerator(0);
Rational item = new Rational();
item.setNumerator(2);
item.setDenominator(1);
while (k <= n) {
sum = sum.add(item);
k++;
int fenzi = item.getNumerator();
int fenmu = item.getDenominator();
item.setNumerator(fenzi + fenmu);
item.setDenominator(fenzi);
}
a = sum.getNumerator();
b = sum.getDenominator();
System.out.println("用分数表示:");
System.out.println(a + "/" + b);
double doubleResult = (a * 1.0) / b;
System.out.println("用小数表示:");
System.out.println(doubleResult);
}
}
![](https://img2018.cnblogs.com/blog/1271708/201903/1271708-20190317173902572-105827685.png)
#上传码云截图
![](https://img2018.cnblogs.com/blog/1271708/201903/1271708-20190317175611589-1255420762.png)
#脚本截图
![](https://img2018.cnblogs.com/blog/1271708/201903/1271708-20190317175633443-419775803.png)