<%@page import="java.util.Random"%><%@ page language="java" contentType="text/html; charset=ISO-8859-1" ???pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>class</title></head><%!public class people//定义一个people类{ ???int height;//身高 ???char name;//呢称 ???//默认构造函数 ???public people(){ ???????height=(int)(Math.random()*40+160); ???????name=(char)(int)(Math.random()*26+‘a‘); ???} ???//有参构造函数 ???public people(int height,char name){ ???????this.height=height; ???????this.name=name; ???} ???//计算标准体重 ???public double getWeight(){ ???????return (this.height-150)*0.6+48; ???} ???//获取身高 ???public double getHeight(){ ???????return this.height; ???} ???//获取呢称 ???public char getName(){ ???????return this.name; ???}}%><body><%//定义两个对象 小红 小明people xiaoming=new people();people xiaohong=new people();//输出小明呢称、身高及标准体重out.print("xiaoming name:"+xiaoming.getName()+"<br>");out.print("xiaoming height:"+xiaoming.getHeight()+"cm<br>");out.print("xiaoming weight:"+xiaoming.getWeight()+"kg<br>");//输出小红呢称、身高及标准体重out.print("xiaohong name:"+xiaohong.getName()+"<br>");out.print("xiaohong height:"+xiaohong.getHeight()+"cm<br>");out.print("xiaohong weight:"+xiaohong.getWeight()+"kg<br>");%></body></html>
Class.jsp
原文地址:https://www.cnblogs.com/sancha/p/10610511.html