如何从主类中的子类获取变量?

时间:2017-04-11 19:01:12

标签: python class oop

 <?php
    include ("login.php")
	session_start();
	if(!isset($_SESSION['Login']))
	{
	   header("Location:login.php");
	   die();
	}
?>

<!DOCTYPE html>
<html lang="en">
  <head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Personal Website</title>
	<link rel="stylesheet" href="../../CSS/stylesheetmain.css">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
	</head>
		<body>
			<!--Navigation Bar-->
			<div class="row">
				<div class="darkgrey column col-md-8 col-md-offset-2 col-xs-12 col-s-12">
					<nav class="navbar navbar-background-color">
					  <div class="container-fluid">
					    <div class="navbar-header">
					      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"><!--Reference: Bootstrap, 2015. getbootstrap.com. [Online] Available at: http://getbootstrap.com/ [Accessed 01 April 2015]-->
					        <span class="sr-only">Toggle navigation</span>
					        <span class="icon-bar"></span>
					        <span class="icon-bar"></span>
					        <span class="icon-bar"></span>
					      </button>
					      <a class="navbar-brand" href="../index.html"></a>
					    </div>
					    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
					      <ul class="nav navbar-nav navbar-right">
                            <li class="active"><a href="../index.html">Home</a></li><!--Edits made: Removed active button -->
                            <li><a href="AboutMe.html">About Me</a></li>
                            <li><a href="Blog.html">Blog</a></li>
                            <li><a href="ContactMe.html">Contact Me</a></li>
                            <li><a href="Login.html">Login</a></li>
                          </ul>
					    </div><!-- /.navbar-collapse -->
					  </div><!-- /.container-fluid -->
					</nav>
				</div>
			</div>
			<div class="row">
				<div class="navbarbottom column col-md-8 col-md-offset-2 col-xs-12 col-s-12"></div>
			</div>
			<!--Page Title-->
			<div class="row">
				<div class="title mediumbluetext col-md-8 col-md-offset-2 col-xs-0 col-s-0">
					<center><h1>Statistics</h1></center>
				</div>
			</div>
			<!--Main Body-->
			<div class="row">
				<!--Left Column Spacer-->
				<div class="maintextleftbackground column col-md-2 col-xs-0 col-s-0">
					<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-52f8f8c0164b330f" async="async"></script>
				</div>
				<!--Middle Column-->
				<div class="maintext80 column col-md-8 col-xs-12 col-s-8 col-s-offset-2"><br>
					<div id="main-chart-container"></div>
					<div id="breakdown-chart-container"></div>
					<div id="embed-api-auth-container"></div>
					<div id="view-selector-container"></div>
				</div>
				<!--Right Column Spacer-->
				<div class="maintextrightbackground column col-md-2 col-xs-0 col-s-0">
				</div>
			</div>
			<!--Footer Bar-->
			<div class="row">
				<div class="darkgrey column col-md-8 col-md-offset-2 col-xs-12 col-s-12">
					<nav class="navbar-background-color">
						<div class="container-fluid">
			              <p class="navbar-text navbar-right"><a href="../SiteMap/SiteMap.html" class="navbar-link">SiteMap</a></p>
						</div>
					</nav>
				</div>
			</div>
    	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
		<script>
			(function(w,d,s,g,js,fs){
			  g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(f){this.q.push(f);}};
			  js=d.createElement(s);fs=d.getElementsByTagName(s)[0];
			  js.src='https://apis.google.com/js/platform.js';
			  fs.parentNode.insertBefore(js,fs);js.onload=function(){g.load('analytics');};
			}(window,document,'script'));
		</script>

</body>	
</html>

我刚开始学习OOP并试图弄清楚如何在练习中使用它。

1 个答案:

答案 0 :(得分:2)

简短回答:这是不可能的(或者,如果它不容易),因为只要class A:开始的阻止没有完成,你就无法访问A

但是您可以将其定义为属性:

class A:
    x = 'variable'
    class B:
        @property
        def x(self):
            return A.x

方法和属性中的代码在调用时执行,因此调用在class A定义后发生。所以你可以使用它:

>>> A.B().x
'variable'

但是,如果您“刚刚开始学习OOP”,我强烈建议您使用嵌套类。尝试关注一些真实的OOP主题,如inheritance