1.如何创建一个类

 public class Person{
//属性
String name;
String genter;
int age;
//方法
public void eat(){
System.out.println("我会吃饭");
}
}

2.使用创建类的步骤

  必须使用new关键字创建一个对象

  使用对象属性(对象名.成员变量)

  使用对象方法(对象名.方法名)

  同一个类的每个对象有不同的成员的成员变量的存储空间

  同一个类的每个对象共享该类的方法

 public class Test1{
public static void main(String[] args){
//创建Person对象
Person one=new Person();
one.name="小明";
System.out.println(one.name);
one.eat(); Person two=new Person();
System.out.println(two.name);
two.eat();
}
}

3.内存分析

 4.定义类的方法时有三种情况,无参无返回、有参无返回、有参有返回

 public class Person{
//属性
String name;
String genter;
int age;
//方法 无参无返回
public void eat(){
System.out.println("我会吃饭");
}
//有参无返回
public void sleep(String s){
System.out.println("在"+s+"睡觉");
}
//有参有返回
public int getAge(int a){
return a;
}
}
 public class Test1{
public static void main(String[] args){
//创建Person对象
Person p1=new Person();
p1.name="小明";
System.out.println(p1.name);
//调用无参无返回方法
p1.eat();
//调用有参无返回方法
p1.sleep("地上");
//调用有参有返回方法
int age=p1.getAge(18);
System.out.println(age); Person p2=new Person();
System.out.println(p2.name);
p2.eat();
p2.sleep("床上");
int age2=p2.getAge(25);
System.out.println(age2); }
}

最新文章

  1. SQL拼接字段数据
  2. three.js贴图
  3. blur和click事件的先后顺序问题
  4. P53 T2
  5. 使用CocoaPod导入Swift第三方库报错
  6. centos下安装php后连接不上mysql
  7. 0511 backlog
  8. Facebook网络模拟测试工具ATC使用
  9. Android Studio-设置switch/case代码块自动补齐
  10. Unity3d 去掉exe版本的边框
  11. OpenStack Nova 制作 Windows 镜像
  12. python开发中常见的小坑
  13. CreateWaitableTimer和SetWaitableTimer函数(定时器)
  14. 应用Java泛型和反射导出CSV文件
  15. bzoj2679:[Usaco2012 Open]Balanced Cow Subsets
  16. Java程序设计---io流读取文件内容并将其逆值输出到控制台
  17. substance在java swing中使用注意事项
  18. ClientURL库-curl_setopt()
  19. tinyproxy代理配置
  20. casperjs批量执行多个url

热门文章

  1. HUST高级软件工程--测试管理工具实践--Day2
  2. Google androd性能优化经典
  3. css笔记-1
  4. C#获取WINDOWS系统信息
  5. .net core webapi +ddd(领域驱动)+nlog配置+swagger配置 学习笔记(1)
  6. WeChat 微信公众号开发步骤
  7. python添加图片验证码
  8. Scrapy 中 Request 对象和 Response 对象的各参数及属性介绍
  9. Centos7 调整磁盘空间
  10. js window.open()打开的页面关闭后刷新父页面