1.面向过程:
最小的处理单位是函数。把大问题分解成小问题,每个问题一个函数来实现。2.面向对象: 最小处理单位是类。把大象装入冰箱,这个过程中有那些事物的行为。3.// 成员变量(python 是在__init__方法中定义的)// 成员变量:在类中定义的变量,属于实例,通过new创建实例的时候,这些属性就创建了。实例销毁时,这些属性才会释放。
// 堆内存。(更新的慢) public/private、static、final可以用来修饰成员变量。
// 局部变量:在方法中定义的变量,或者方法的参数。调用方法的时候,才会创建局部变量,方法调用完释放。
// 栈内存。(更新的快) final可以用来修饰局部变量。
//成员变量和局部变量的名字可以一样。
// void 表示没有返回值
// public 表示公共的
// () 里面没有内容,表示没有参数
public void sleeping() {
System.out.println(name + "正在睡觉");
}
// 方法带了两个String类型的参数,一个表示地点,一个表示书
public void learning( String place,String book){
System.out.println(name + "在" + place + "看" + book);
}
// 方法声明返回一个int类型的值,方法体中用return返回对应类型的值。
public int getAge(){
return age;
}
public float getWeight(){
return weight;
}package day03;
/*
圆类:
属性:半径r
行为:面积、周长
Math.PI
*/
public class Demo02 {
public static void main(String[] args) {
Circle c1 = new Circle();
c1.r = 5;
System.out.println("半径为:" + c1.r + ",面积:" + c1.mianJi() + ",周长:" + c1.zhouChang());
Circle c2 = new Circle();
c2.r = 15;
System.out.println("半径为:" + c2.r + ",面积:" + c2.mianJi() + ",周长:" + c2.zhouChang());
}
}
class Circle {
float r;
public double mianJi() {
return r * r * Math.PI;
}
public double zhouChang () {
return 2 * r * Math.PI;
}
}方法的重载:一个类中有多个重名的方法:参数的个数,参数的顺序,参数的类型不同;返回值不同,不构成重载。public String add (int a ,String b){
System.out.println("add(int a ,String b)");
return a + b;
}
public static void main(String[] args) {
Demo03 d03 = new Demo03();
// python 弱类型语言,方法中定义两个参数,调用时可以传任意类型的。
//java 强类型的语言,方法中变量的类型是确定的,调用时只能传对应类型的值。
// 重载的好处:调用起来比较简单。
d03.add (10,20); // int
d03.add (10.0f,20.0f); // float
d03.add (10.0,20.0,30.0,50); // doule
d03.add ("10","20");
d03.add ("10",20);
d03.add (10,"20");
// System.out.println 是重载的方法,只要知道打印的内容,知道这个方法名即可
}
}构造方法:
1.与类名相同的方法,没有返回值,不能有return语句.
2.类里面没有写构造方法,系统会默认提供一个无参的构造方法。
3.类里面如果有构造方法时,系统默认的构造方法就不存在了
4.构造方法支持重载,创建实例时更灵活。
5.构造方法可以用IDE自动生成定义一个三角形的类: Triangle
属性:a,b,c
方法: 构造方法,Triangle().toString().zhongChang().type():返回一般三角形,等边,等腰,不是三角形
*/
public class Demo05 {
public static void main(String[] args) {
Triangle t1 = new Triangle(1,2,3);
System.out.println(t1);
Triangle t2 = new Triangle(2,2,3);
System.out.println(t2);
Triangle t3 = new Triangle(3,5,1);
System.out.println(t3);
Triangle t4 = new Triangle(3,4,5);
System.out.println(t4);
}
}
class Triangle {
float a, b, c;
public Triangle(float a, float b, float c) {
this.a = a;
this.b = b;
this.c = c;
}
public float zhouChang() {
//返回三角形的和
return a + b + c;
}
public String type() {
//如果 两边之和小于第三边,不是三角形
//如果 三遍相等,等边
//如果任意两边相等 等腰
//否则,一般三角形
if (a + b < c ||a+c<b||b+c<a) {
return "不是三角形";
} else if (a == b && b == c) {
return "等边三角形";
} else if (a == b || b == c || c == a) {
return "等腰三角形";
} else {
return "一般三角形";
}
对象数组、对象参数、可变参数。
*/
import java.util.Arrays;
public class Demo06 {
public static void main(String[] args) {
// 新建3个学生,把学生加入数组
Students s1 = new Students("Lily",50);
Students s2 = new Students("Lucy",55);
Students s3 = new Students("Tony",98);
Students[] ss = {s1,s2,s3};
System.out.println(Arrays.toString(ss));
modifyScore(ss);//调用静态方法,Demo06.modifyScore 类名.方法名 方式调用。同一个类中类名可以省略。
System.out.println(Arrays.toString(ss));
Students s4 = new Students("Jack",50);
System.out.println(s4);
modifyScore(s4);
System.out.println(s4);
Students s5 = new Students("Lily",50);
Students s6 = new Students("Lucy",55);
modifyScore1(s5,s6);
System.out.println(s5);
System.out.println(s6);
}
// 修改学生的分数,如果小于60,将分数提高到60分
public static void modifyScore(Students stu){
if(stu.score <60){
stu.score = 60;
}
}
// 批量修改学生的分数
public static void modifyScore(Students[] stu) {
for(Students s : stu){
if (s.score <60){
s.score = 60;
}
}
}
// 可变参数,当作一个数组来看,可以传入0个,1个,2 个....n个学生。
public static void modifyScore1(Students ...stu) {
for(Students s : stu){
if (s.score <60) {
s.score = 60;
}
}
}
}
class Students{
String name;
float score;
public Students(String name, float score) {
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Students{" +
"name='" + name + '\'' +
", score=" + score +
'}';
}
}
|