JavaSE,
分享于 点击 16563 次 点评:90
JavaSE,
面向对象
public class Person {
static String name;
int age;
char sex;
//静态代码块 伪静态变量赋值
static {
name = "李金克";
System.out.println("调用了静态代码块");
}
//非静态代码块
{
}
//无参构造方法
public Person() {
}
//有参构造方法
public Person(String name) {
this.name = name;
}
//全参构造
public void huXi() {
System.out.println("呼吸");
}
public Person(int age, char sex) {
super();
this.age = age;
this.sex = sex;
}
public Person(String name, int age, char sex) {
super();
//this.name = name;
this.age = age;
this.sex = sex;
}
public void eat() {
System.out.println(name + "吃饭");
}
public void sleep() {
System.out.println("睡觉");
}
public static void main(String[] args) {
Person xw = new Person();
Person.name = "xiaoming";
xw.age = 5;
//xw.name = "小五";
xw.sex = 'M';
xw.eat();
xw.huXi();
xw.sleep();
}
}
public class Point {
int x;
int y;
public Point (int x ,int y) {
this.x = x;
this.y = y;
}
public double distance (int a, int b) {
return Math.sqrt(Math.pow(x - a, 2)+Math.pow(y - b, 2));
}
public double distance (Point p) {
return Math.sqrt(Math.pow(x - p.x, 2)+Math.pow(y - p.y, 2));
}
public static void main(String[]args) {
Point p1 = new Point(1,1);
Point p2 = new Point(4,5);
System.out.println(p1.distance(p2));
}
}
package com.sxt;
public class MathUtil {
public static double add(double a, double b) {
double sum = a + b;
return sum;
}
public static double subtract(double a ,double b) {
double subtract = a - b;
return subtract;
}
public static double multiply(double a ,double b) {
double multiply = a - b;
return multiply;
}
public static double divide(double a, double b) {
double divide = a / b;
return divide;
}
public static void main(String[] args) {
double a = MathUtil.add(3.2,9.6);
double b = MathUtil.subtract(6.4, 6.1);
double c = MathUtil.multiply(23, 6);
double d = MathUtil.divide(5, 2);
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
}
- 4 .
package com.sxt;
import java.math.BigInteger;
public class Teacher {
String name;
int age;
String place;
String telphone;
public Teacher(String name, int age, String place, String telphone) {
super();
this.name = name;
this.age = age;
this.place = place;
this.telphone = telphone;
}
public void teach() {
System.out.println("教学内容");
}
public void test() {
System.out.println("考试");
}
public void introduce() {
System.out.println("自我介绍:"+name+age+place+telphone);
}
public static void main(String[] args) {
Teacher Lxs = new Teacher("李先念",30,"河南郑州","1234567890");
Teacher Wxs = new Teacher("王阳明",45,"河南洛阳","9876543210");
Lxs.introduce();
Lxs.teach();
Lxs.test();
Wxs.introduce();
Wxs.teach();
Wxs.test();
}
}
相关文章
- 暂无相关文章
用户点评