综合案例讲解 与 static关键字
public class OOPDemo01 { public static void main(String[] args) { Rectangle r = new Rectangle(10, 30); System.out.println("长方形的周长:"+r.getPermeter()); System.out.println("长方形的面积:"+r.getArrea()); } } class Rectangle{ private int length; private int width; public Rectangle() { } public Rectangle(int length, int width) { this.length = length; this.width = width; } public int getLength() { return length; } public void setLength(int length) { this.length = length; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } /* 获取周长 */ public int getPermeter(){ return this.length*2 + this.width*2; } /* 获取面积方法 */ public int getArrea(){ return this.length*this.width; } }
2、封装一个学生类,有姓名,有年龄,有性别,有英语成绩,数学成绩,语文成绩,封装方法,求总分,平均分,以及打印学生的信息。
public class OOPDemo02 { public static void main(String[] args) { Student s = new Student("张三",20,"男",88,90,60); System.out.println(s.getName()+"的总分:"+s.getTotalScore()); System.out.println(s.getName()+"的平均分:"+s.getAvg()); s.printStudent(); } } class Student{ private String name; private int age; private String gender; private double english; private double math; private double chinese; public Student() { } public Student(String name, int age, String gender, double english, double math, double chinese) { this.name = name; this.age = age; this.gender = gender; this.english = english; this.math = math; this.chinese = chinese; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public double getEnglish() { return english; } public void setEnglish(double english) { this.english = english; } public double getMath() { return math; } public void setMath(double math) { this.math = math; } public double getChinese() { return chinese; } public void setChinese(double chinese) { this.chinese = chinese; } /* 获取总分 */ public double getTotalScore(){ return this.english+this.math+this.chinese; } /* 获取平均分 */ public double getAvg(){ return this.getTotalScore()/3; } /* 打印学生的信息 */ public void printStudent(){ System.out.println("name="+this.name + "\t" +"age="+this.age + "\t" +"gender="+this.gender+ "\t" +"english="+this.english+ "\t" +"math="+this.math+ "\t" +"chinese="+this.chinese + "\t"); } }
3、定义一个“点”(Point) x y set get类用来表示二维空间中的点。要求如下:
A可以生成具有特定坐标的点对象。
C提供可以计算该点距离另一点距离的方法。
D提供可以计算三个点构成图形的面积的方法。
面积可以使用海伦公式:边长分别为:a,b,c p=(a+b+c)/2
s=Math.sqrt(p*(p*a)(p*b)(p*c))
public class OOPDemo03 { public static void main(String[] args) { Point p1 = new Point(1, 1); Point p2 = new Point(2, 1); Point p3 = new Point(2, 2); System.out.println("p1,p2,p3三点之间构成的图案的面积是:"+p1.getArea(p1,p2,p3)); } } class Point{ private double x; private double y; public Point() { } public Point(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } /* 获取两个点之间的距离 */ public double getDistance(Point p){ double _x = this.x - p.getX(); double _y = this.y - p.getY(); return Math.sqrt(_x*_x + _y*_y); } /* 任意两个点之间的距离 */ public double getDistance(Point p1,Point p2){ double _x = p1.x - p2.getX(); double _y = p1.y - p2.getY(); return Math.sqrt(_x*_x + _y*_y); } /* */ public double getArea(Point p1,Point p2,Point p3){ double a = getDistance(p1,p2); double b = getDistance(p3,p2); double c = getDistance(p1,p3); double p = (a+b+c)/2; return Math.sqrt(p*(p-a)*(p-b)*(p-c)); } }
特点:
-
静态变量属于某个类,而不属于某个具体的对象
-
只有静态才能访问静态,非静态变量不能出现在静态方法中
-
访问静态成员的方式
-
类名.成员变量
-
类名.成员方法
-
-
静态环境下不能出现this和super关键字
-
static能修饰的成员有:成员变量,成员方法,修饰类 的是内部类
-
我们开发自己的工具类的时候会经常用到static
public class a2 { public static void main(String[] args) { User user = new User("张三",20); User user1 = new User("张三1",10); User user2 = new User("张三2",40); User.country = "中国"; user.show(); user1.show(); user2.show(); } } class User{ private String name; private int age; public static String country; public void show(){ System.out.println("name"+name+"age:"+age+"country"+ country); } public static void showNew(){ //调用不了会报错不能调用非静态的方法 System.out.println(name); } public User() { } public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix